如何获得* nix进程的文件名

我怎样才能找出正在运行的进程的程序文件名是什么?

以下从shell运行将为您提供所有正在运行的程序的最后一列中的命令,完整path和调用参数:

ps -eF 

这是unix的语法,因为你不是特定的。 在Linux中也有GNU和BSD语法。 man ps了解更多。

请注意,上述所有命令只能在一些时间内运行 。 例如,在这里“ps”的输出显示了一个程序的path,但是如果你尝试访问那个path,你会发现没有任何东西:

  $ ./myprogram & $ rm myprogram $ ps -fe | grep myprogram lars 27294 29529 0 20:39 pts/1 00:00:00 ./myprogram $ ls myprogram ls: myprogram: No such file or directory 

实际上,ps显示的值完全取决于程序启动的任何代码 。 例如:

 $ python -c "import os; os.execl('./myprogram', '/usr/sbin/sendmail')" & myprogram: i am: 27914 $ ps -f -p 27914 UID PID PPID C STIME TTY TIME CMD lars 27914 29529 0 20:44 pts/1 00:00:00 /usr/sbin/sendmail 

所以基本上,你不能依赖ps的输出。 您可能可以依赖/proc/PID/exe ,例如:

  $ ls -l /proc/27914/exe lrwxrwxrwx 1 lars lars 0 Dec 3 20:46 /proc/27914/exe -> /home/lars/tmp/myprogram 

但即使在这种情况下,该文件可能不再存在。

这样的事情呢?

lsof -p pid | grep'txt'

男人lsof

 -ps This option excludes or selects the listing of files for the processes whose optional process IDentification (PID) numbers are in the comma-separated set ... FD is the File Descriptor number of the file or: txt program text (code and data); 

一般来说,如果你正在从shell中寻找,你可以使用ps

如果您的程序深深地位于/ usr / local / program / bin文件系统树中,那么在有限的terminal上可能不会看到完整的path和程序。

您可以使用:

 ps -auxww 

看到属于所有进程的完整无限的命令行。

出了ps的man手册:

 w Wide output. Use this option twice for unlimited width. 

在Linux上它非常简单:

 $ cat /proc/4670/cmdline kdeinit4: plasma-desktop [kdeinit] 

除非你确定没有别的办法,否则不要分析任何东西。

干杯! 🙂