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 aspgrepso 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.pkillwhen using-f(which he is doing) will have the same problem.-xin combination with-fto make sure you are matching the whole thing? I'm surprisedpkilldoesn't have a built in way to handle this.pkillkill itself before killing the processes I wanted to kill, so I'd always assumed the implementation was good.