为什么 pidof 不起作用?

为什么 pidof 不起作用?

在 的输出中ps aux,我可以很好地看到该过程:

# ps aux | grep diff
root      7787 28.7  0.0   9368  4516 pts/3    D+   13:56  20:33 diff -qr mnt/mnt/md/ mnt/mnt2/
root     13130  0.0  0.0   6144   876 pts/4    S+   15:07   0:00 grep diff

pidof声称找不到任何东西:

# pidof diff
# echo $?
1

查看手册页,没有关于丢失进程时该怎么做的信息,pidof 有。/proc/7787/exe是一个符号链接/usr/bin/diff/usr/bin/diff它本身是一个常规文件和一个 ELF。根据手册页,这应该匹配。

答案1

经过一番调查后strace,似乎pidof还检查了进程的状态。我的进程大部分时间diff都处于状态,这意味着它正在大量等待 I/O。D有了这些知识,我跑了pidof很多次(大约 3 秒内):

# pidof diff
7787
# pidof diff
# pidof diff
7787
# pidof diff
# pidof diff
# pidof diff
# pidof diff
# pidof diff
# pidof diff
# pidof diff
# pidof diff
# pidof diff
# pidof diff
7787
# pidof diff
7787

pidof它确实“有时”返回它,似乎证实了只返回未处于状态的进程的怀疑D

pidof检查in的源代码src/killall5.c(使用 获得apt source sysvinit-utils),答案在第599行:

if ( (strchr(process_status, 'D') != NULL) ||
     (strchr(process_status, 'Z') != NULL) ){
   /* Ignore zombie processes or processes in
      disk sleep, as attempts
      to access the stats of these will
      sometimes fail. */

答案2

在 sysvinit 2.96 及更高版本中,pidof -z将包括处于磁盘 I/O ('D') 或僵尸 ('Z') 状态的进程。

相关内容