I want to kill the following processes using
pkill "run_tcp_sender.sh"
or
pkill -SIGKILL "run_tcp_sender.sh"
root 14320 1 0 2012 ? 00:00:00 bash run_tcp_sender.sh 138.96.116.22
root 14323 1 0 2012 ? 00:00:00 bash run_tcp_sender.sh 138.96.116.22
root 14325 1 0 2012 ? 00:00:00 bash run_tcp_sender.sh 138.96.116.22
root 14327 1 0 2012 ? 00:00:00 bash run_tcp_sender.sh 138.96.116.22
root 14328 1 0 2012 ? 00:00:00 bash run_tcp_sender.sh 138.96.116.22
root 14330 1 0 2012 ? 00:00:00 bash run_tcp_sender.sh 138.96.116.22
but it is useless
the processes remain there
what is wrong with my command?
BTW:
I can use the following command to achieve what I want
kill -9 $(ps -ef|grep "run_tcp"|grep -v "grep"|awk '{print $2}')
Best Answer
pkill
by default sends theSIGTERM
signal to processes to stop. Here's a list of the signals you can send a process. You can send them by name or number typically:So you're sending signal # 15. If the processes are not responding to this signal then you may need to use signal #9,
pkill -SIGKILL
.From the man page of pkill:
Issues with pkill
The OP mentioned that he was unsuccessful in getting
pkill -SIGKILL "run_tcp"
to work. We initially thought that the issue had to do withpkill
potentially killing itself before it had finished killing all the "run_tcp" processes.But that was hard to accept given a foot note in the
pkill
man page:In addition to that, @Gilles left a comment basically saying the same thing, that
pkill
just does not kill itself. Then he gave us a pretty big clue as to what was actually going on.Here's an example that demonstrates what the OP and myself were missing:
step 1 - make a sleepy.bash script
step 2 - load up some fake sleep tasks
step 3 - check the running tasks
step 4 - try using my pkill
step 5 - what happened?
Doing the
ps
command from above, we see that none of the processes were killed, just like the OPs issue. What's going on?Turns out this is an issue in how we were attempting to make use of
pkill
. The command:was looking for a process by the name of "sleepy.bash". Well there aren't any processes by that name. There's processes that are named "bash sleepy.bash" though. So
pkill
was looking for processes to kill and not finding any and then exiting.So if we slightly adjust the
pkill
we're using to this:Now we get the effect we were looking for. What the difference? We made use of the
-f
switch topkill
which makespkill
use the entire command line path when matching vs. just the process name.from pkill man page
Alternative methods
kill, ps
This method is pretty verbose but does the job as well:
pgrep w/ pkill & killall
You can use either
pgrep
to feed a list of PIDs topkill
or make use ofkillall
instead.Examples
References