Linux – killall gives me `no process found ` but ps

killlinuxprocessps

Could somebody explain to me the difference between kill and killall? Why doesn't killall see what ps shows?

# ps aux |grep db2
root      1123  0.0  0.8 841300 33956 pts/1    Sl   11:48   0:00 db2wdog                                         
db2inst1  1125  0.0  3.5 2879496 143616 pts/1  Sl   11:48   0:02 db2sysc                                        
root      1126  0.0  0.6 579156 27840 pts/1    S    11:48   0:00 db2ckpwd                                        
root      1127  0.0  0.6 579156 27828 pts/1    S    11:48   0:00 db2ckpwd                                        
root      1128  0.0  0.6 579156 27828 pts/1    S    11:48   0:00 db2ckpwd 

# killall db2ckpwd
db2ckpwd: no process found

# kill -9 1126
# kill -9 1127
# kill -9 1128

System is SuSe 11.3 (64 bit); kernel 2.6.34-12; procps version 3.2.8; killall from PSmisc 22.7; kill from GNU coreutils 7.1

Best Answer

Is this on Linux?

There are actually a few subtly different versions of the command name that are used by ps, killall, etc.

The two main variants are: 1) the long command name, which is what you get when you run ps u; and 2) the short command name, which is what you get when you run ps without any flags.

Probably the biggest difference happens if your program is a shell script or anything that requires an interpreter, e.g. Python, Java, etc.

Here's a really trivial script that demonstrates the difference. I called it mycat:

#!/bin/sh
cat

After running it, here's the two different types of ps.

Firstly, without u:

$ ps -p 5290
  PID TTY      ... CMD
 5290 pts/6    ... mycat

Secondly, with u:

$ ps u 5290
USER       PID ... COMMAND
mikel     5290 ... /bin/sh /home/mikel/bin/mycat

Note how the second version starts with /bin/sh?

Now, as far as I can tell, killall actually reads /proc/<pid>/stat, and grabs the second word in between the parens as the command name, so that's really what you need to be specifying when you run killall. Logically, that should be the same as what ps without the u flag says, but it would be a good idea to check.

Things to check:

  1. what does cat /proc/<pid>/stat say the command name is?
  2. what does ps -e | grep db2 say the command name is?
  3. do ps -e | grep db2 and ps au | grep db2 show the same command name?

Notes

If you're using other ps flags too, then you might find it simpler to use ps -o comm to see the short name and ps -o cmd to see the long name.

You also might find pkill a better alternative. In particular, pkill -f tries to match using the full command name, i.e. the command name as printed by ps u or ps -o cmd.

Related Question