iga*_*gal 4 linux process ps proc
在调试相关问题时,我注意到 pgrep 正在为看似任意的命令行模式返回一个 PID,例如:
$ sudo pgrep -f "asdf"
13017
$ sudo pgrep -f ";lkj"
13023
$ sudo pgrep -f "qwer"
13035
$ sudo pgrep -f "poiu"
13046
$ sudo pgrep -f "blahblahblah"
14038
$ sudo pgrep -f "$(pwgen 16 1)"
14219
Run Code Online (Sandbox Code Playgroud)
没有 sudo 的相同命令没有返回任何内容(如预期):
$ pgrep -f blahblahblah
Run Code Online (Sandbox Code Playgroud)
我试图通过管道将 PID 传递给 ps 以查看命令是什么,但这不起作用:
$ sudo pgrep -f blahblahblah | xargs ps -f -p
UID PID PPID C STIME TTY TIME CMD
Run Code Online (Sandbox Code Playgroud)
看起来进程终止得太快了。然后我尝试使用 ps 和 grep,但这也不起作用(即没有结果):
$ sudo ps -e -f | grep [a]sdf
$ sudo ps -e -o command | grep asdf
grep asdf
Run Code Online (Sandbox Code Playgroud)
我还注意到,如果我足够快地重新运行命令,那么似乎 PID 正在稳步攀升:
$ for i in $(seq 1 10); do sudo pgrep -f $(pwgen 4 1); done
14072
14075
14078
14081
14084
14087
14090
14093
14096
14099
$ for i in $(seq 1 10); do sudo pgrep -f blahblahblah; done
13071
13073
13075
13077
13079
13081
13083
13085
13087
13089
Run Code Online (Sandbox Code Playgroud)
作为完整性检查,我尝试使用 find 和 grep 来搜索 proc 目录:
$ sudo find /proc/ -regex '/proc/[0-9]+/cmdline' -exec grep adsfasdf {} \;
Binary file /proc/14113/cmdline matches
Binary file /proc/14114/cmdline matches
$ sudo find /proc/ -regex '/proc/[0-9]+/cmdline' -exec grep adsfasdf {} \;
Binary file /proc/14735/cmdline matches
Binary file /proc/14736/cmdline matches
Run Code Online (Sandbox Code Playgroud)
同样,PID 似乎在攀升,并且 cmdline 与任意字符串匹配。
我在 CentOS 6.7 和 Ubuntu 12.04 上都试过了,结果相同。当我在我的 Mac 上尝试类似的实验时,测试结果是否定的 - 没有神秘的过程。
这里发生了什么?
它显示了sudo
进程,即 PID 是进程的 PID,该sudo
进程是pgrep
您正在运行的命令的父进程,将sudo
. 当您在整个命令行 (by -f
)中搜索时,该sudo
过程会在输出中弹出,因为字符串(模式)也是原始sudo
命令的一部分。
通过使用-l
和-a
(如果你pgrep
支持),你会得到一个更好的主意。
测试:
% sudo pgrep -af "asdf"
4560 sudo pgrep -af asdf
% sudo pgrep -lf "asdf"
4562 sudo
% pgrep -af "asdf"
%
Run Code Online (Sandbox Code Playgroud)