My script accepts a process name as an input and kills it. I tried using pgrep but it's returning two PIDs , one for the process which is running and one for the script which accepts the process name as input, so am stuck! I tried using the pgrep
-fo
option too but that did not help either. Any suggestions would be helpful.
This is my script
#!/bin/bash
ProcessName=$1
pID= pgrep -fl $ProcessName
echo $pID
So, when I invoke the script, it's returning two PIDs:
bash-3.00$ ./dynamic_values.sh test-Process
10534 /xxx/xxo/xxx/xxe --run --propFile /application/test/test-Process_Archive.tra --innerProcess
23401 /bin/bash ./dynamic_values.sh test-Process
I was expecting just 10534, but it picked up the script too . Version of OS just in case:
bash-3.00$ uname -a
Linux xxxxxx 2.6.9-67.0.1.
pkill
. It uses the same process findng logic aspgrep
so you already know how to use it to find your process, but rather than returning the PID's to you, it just goes ahead and sends them a kill signal for you. In the mean time, it also takes care of details such as not killing itself. – Caleb Aug 29 '12 at 10:20pkill
when using-f
(which he is doing) will have the same problem. – bahamat Aug 29 '12 at 15:30-x
in combination with-f
to make sure you are matching the whole thing? I'm surprisedpkill
doesn't have a built in way to handle this. – Caleb Aug 29 '12 at 16:24pkill
kill itself before killing the processes I wanted to kill, so I'd always assumed the implementation was good. – jw013 Aug 29 '12 at 18:40pkill
andpgrep
are identical except that one prints the pids and the other one sends a signal. They take care never to match themselves, but they can match their caller. – Gilles 'SO- stop being evil' Aug 29 '12 at 22:50