检查一个进程是否是Go应用程序(在Linux/Unix中)

huangapple go评论81阅读模式
英文:

Check if a process is a Go application (in Linux/Unix)

问题

我正在使用ps(具体来说是psutil Python库)来查看我Linux机器上正在运行的进程列表。我想确定哪些进程正在运行Go应用程序(也就是说,它们正在运行Go代码)。

下面是一个用Go编写的进程的ps输出。有没有任何指示它正在运行Go代码的东西?

{'cmdline': ['./SampleMePlz'],
 'connections': [connection(fd=3, family=10, type=1, laddr=('::', 8080), raddr=(), status='LISTEN')],
 'cpu_affinity': [0, 1, 2, 3],
 'cpu_percent': 0.0,
 'cpu_times': cputimes(user=0.0, system=0.0),
 'create_time': 1436474176.89,
 'cwd': '/home/ben/gocode/src/SampleMePlz',
 'exe': '/home/ben/gocode/src/SampleMePlz/SampleMePlz',
 'ext_memory_info': meminfo(rss=4071424, vms=192684032, shared=3092480, text=3096576, lib=0, data=179523584, dirty=0),
 'gids': group(real=5000, effective=5000, saved=5000),
 'io_counters': io(read_count=12, write_count=0, read_bytes=4096, write_bytes=0),
 'ionice': ionice(ioclass=0, value=4),
 'memory_info': meminfo(rss=4071424, vms=192684032),
 'memory_maps': [mmap(path='/lib/x86_64-linux-gnu/libpthread-2.15.so', rss=65536, size=2199552, pss=8192, shared_clean=57344, shared_dirty=0, private_clean=0, private_dirty=8192, referenced=65536, anonymous=8192, swap=0),
                 mmap(path='/lib/x86_64-linux-gnu/libc-2.15.so', rss=311296, size=3903488, pss=26624, shared_clean=286720, shared_dirty=0, private_clean=0, private_dirty=24576, referenced=311296, anonymous=24576, swap=0),
                 mmap(path='[stack]', rss=8192, size=139264, pss=8192, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=8192, referenced=8192, anonymous=8192, swap=0),
                 mmap(path='[anon]', rss=946176, size=179245056, pss=946176, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=946176, referenced=946176, anonymous=946176, swap=0),
                 mmap(path='/lib/x86_64-linux-gnu/ld-2.15.so', rss=122880, size=151552, pss=13312, shared_clean=110592, shared_dirty=0, private_clean=0, private_dirty=12288, referenced=122880, anonymous=12288, swap=0),
                 mmap(path='[vsyscall]', rss=0, size=4096, pss=0, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=0, referenced=0, anonymous=0, swap=0),
                 mmap(path='[heap]', rss=4096, size=135168, pss=4096, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=4096, referenced=4096, anonymous=4096, swap=0),
                 mmap(path='[vdso]', rss=4096, size=4096, pss=0, shared_clean=4096, shared_dirty=0, private_clean=0, private_dirty=0, referenced=4096, anonymous=0, swap=0),
                 mmap(path='/home/ben/gocode/src/SampleMePlz/SampleMePlz', rss=2908160, size=6905856, pss=2908160, shared_clean=0, shared_dirty=0, private_clean=2879488, private_dirty=28672, referenced=2908160, anonymous=28672, swap=0)],
 'memory_percent': 0.025919821179310142,
 'name': 'SampleMePlz',
 'nice': 0,
 'num_ctx_switches': amount(voluntary=15, involuntary=18),
 'num_fds': 5,
 'num_threads': 6,
 'open_files': [],
 'pid': 111252,
 'ppid': 42061,
 'status': 'sleeping',
 'terminal': '/dev/pts/9',
 'threads': [thread(id=111252, user_time=0.0, system_time=0.0),
             thread(id=111253, user_time=0.0, system_time=0.0),
             thread(id=111254, user_time=0.0, system_time=0.0),
             thread(id=113972, user_time=0.0, system_time=0.0),
             thread(id=113973, user_time=0.0, system_time=0.0),
             thread(id=113974, user_time=0.0, system_time=0.0)],
 'uids': user(real=3008, effective=3008, saved=3008),
 'username': 'ben'}
英文:

I am using ps (specifically the psutil python library) to see a list of running processes on my linux machine. I want to deterimine which processes are running Go applications (that is, they are running Go code).

Below is the ps output for a process that is written in Go. Is there anything that indicates that it is running Go code?

{'cmdline': ['./SampleMePlz'],
 'connections': [connection(fd=3, family=10, type=1, laddr=('::', 8080), raddr=(), status='LISTEN')],
 'cpu_affinity': [0, 1, 2, 3],
 'cpu_percent': 0.0,
 'cpu_times': cputimes(user=0.0, system=0.0),
 'create_time': 1436474176.89,
 'cwd': '/home/ben/gocode/src/SampleMePlz',
 'exe': '/home/ben/gocode/src/SampleMePlz/SampleMePlz',
 'ext_memory_info': meminfo(rss=4071424, vms=192684032, shared=3092480, text=3096576, lib=0, data=179523584, dirty=0),
 'gids': group(real=5000, effective=5000, saved=5000),
 'io_counters': io(read_count=12, write_count=0, read_bytes=4096, write_bytes=0),
 'ionice': ionice(ioclass=0, value=4),
 'memory_info': meminfo(rss=4071424, vms=192684032),
 'memory_maps': [mmap(path='/lib/x86_64-linux-gnu/libpthread-2.15.so', rss=65536, size=2199552, pss=8192, shared_clean=57344, shared_dirty=0, private_clean=0, private_dirty=8192, referenced=65536, anonymous=8192, swap=0),
                 mmap(path='/lib/x86_64-linux-gnu/libc-2.15.so', rss=311296, size=3903488, pss=26624, shared_clean=286720, shared_dirty=0, private_clean=0, private_dirty=24576, referenced=311296, anonymous=24576, swap=0),
                 mmap(path='[stack]', rss=8192, size=139264, pss=8192, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=8192, referenced=8192, anonymous=8192, swap=0),
                 mmap(path='[anon]', rss=946176, size=179245056, pss=946176, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=946176, referenced=946176, anonymous=946176, swap=0),
                 mmap(path='/lib/x86_64-linux-gnu/ld-2.15.so', rss=122880, size=151552, pss=13312, shared_clean=110592, shared_dirty=0, private_clean=0, private_dirty=12288, referenced=122880, anonymous=12288, swap=0),
                 mmap(path='[vsyscall]', rss=0, size=4096, pss=0, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=0, referenced=0, anonymous=0, swap=0),
                 mmap(path='[heap]', rss=4096, size=135168, pss=4096, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=4096, referenced=4096, anonymous=4096, swap=0),
                 mmap(path='[vdso]', rss=4096, size=4096, pss=0, shared_clean=4096, shared_dirty=0, private_clean=0, private_dirty=0, referenced=4096, anonymous=0, swap=0),
                 mmap(path='/home/ben/gocode/src/SampleMePlz/SampleMePlz', rss=2908160, size=6905856, pss=2908160, shared_clean=0, shared_dirty=0, private_clean=2879488, private_dirty=28672, referenced=2908160, anonymous=28672, swap=0)],
 'memory_percent': 0.025919821179310142,
 'name': 'SampleMePlz',
 'nice': 0,
 'num_ctx_switches': amount(voluntary=15, involuntary=18),
 'num_fds': 5,
 'num_threads': 6,
 'open_files': [],
 'pid': 111252,
 'ppid': 42061,
 'status': 'sleeping',
 'terminal': '/dev/pts/9',
 'threads': [thread(id=111252, user_time=0.0, system_time=0.0),
             thread(id=111253, user_time=0.0, system_time=0.0),
             thread(id=111254, user_time=0.0, system_time=0.0),
             thread(id=113972, user_time=0.0, system_time=0.0),
             thread(id=113973, user_time=0.0, system_time=0.0),
             thread(id=113974, user_time=0.0, system_time=0.0)],
 'uids': user(real=3008, effective=3008, saved=3008),
 'username': 'ben'}

答案1

得分: 4

简短的回答是不可以。代码被编译后,不会留下任何关于使用何种语言/编译器的信息。你可以寻找一些Go程序的特征,比如对GOPATH或其子目录的引用,但并不能得到更多的信息。

英文:

The short answer is no. The code is compiled leaving no information about what language/compiler produced it. You can look for symptoms of a go-program, like references to your GOPATH or sub directories of it, but not much more.

答案2

得分: 1

除了@evanmcdonnal的答案之外,这里有一个(非常非常)笨拙的解决方案:

strings /path/to/binary/from/ps/output | grep 'runtime.gogc' 

输出:

<snip>
runtime.gogc                                                                                            runtime.gogc   
runtime.gogc   
<snip>

当然,这种方法依赖于二进制格式不会改变或以其他方式被混淆,这是一个不好的长期假设。我还假设runtime.gogc在所有Go二进制文件中都存在(可能不是这样)。

请注意,从操作角度来看,这些信息很少有用。

英文:

In addition to @evanmcdonnal's answer, here's a (very, very) hacky solution:

strings /path/to/binary/from/ps/output | grep &#39;runtime.gogc&#39; 

Output:

&lt;snip&gt;
runtime.gogc                                                                                            runtime.gogc   
runtime.gogc   
&lt;snip&gt;

This of course relies on the binary format not changing or otherwise being obfuscated, which is not a good assumption to make long-term. I'm also assuming that runtime.gogc exists across all Go binaries (it may not).

Note that it would be very rare for this to be useful information from an operational point of view.

huangapple
  • 本文由 发表于 2015年7月10日 05:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/31328213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定