2

In the output of ps aux, I can see the process just fine:

# 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

But pidof claims not to be able to find anything:

# pidof diff
# echo $?
1

Looking at the man page, there is no info on what to do when lost a process, pidof has. /proc/7787/exe is a symlink to /usr/bin/diff and /usr/bin/diff itself is a regular file and an ELF. According to the man page, this ought to match.

4

After some investigation with strace, it seems that pidof also checks the status of the processes. My diff process was in D state most of the time, meaning it is waiting for I/O a lot. With this knowledge, I ran pidof a bunch of times (this is within ~3 seconds):

# 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

It indeed returns it 'sometimes', seeming to confirm the suspicion that pidof only return processes that are not in D state.

Checking the source code of pidof in src/killall5.c (obtained using apt source sysvinit-utils), the answer lies on line 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
  • So rather than sometimes failing, they elect to always fail on processes in those states. And pidof doesn't use any stats anyway, just shows the process ID. It can surely check the process ID and exe name, in fact this little shell command does it: ls -l /proc/*/exe 2>/dev/null | grep "/$WHAT$" Am I missing something? I'd like to do a git blame on this one and ask them a few pointed questions. May 3 at 1:59
  • @SamWatkins By all means! I see it the same way.
    – Luc
    May 3 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.