Multiple process found when executed from script

Hi All,

I came across a weird scenario which i couldnt explain why.
As part of script which is run as /bin/sh shebang, we are trying to check if there is any process that runs with the same name ignoring the current process and kill it.
But for some reason, there are 2 process returned as part of echo and kill command for same process name. But if we do just ps -edf we just get one.

Please find belwo the commands

 
 echo ` ps -edf | grep process_name | grep -v pattern | grep \? | grep -v grep `
  
 kill -9 `ps -edf | grep process_name | grep -v  pattern | grep \? | grep -v grep | nawk '{print $2}' | sed "s/$$//g"`
 

Both the above command returns 2 process(the output of command within ``), one is the current process which is created when the script is executed and the second one comes as a child of the first process but with same command. The second process gets killed forcefully and its causing a failure trigger for my job which uses that script.

If i use below command on the same script, i just get the current process.

ps -edf | grep process_name | grep -v pattern | grep \? | grep -v grep 

Any inputs is highly appreciated

Regards,
Pradeep

Try the pkill command pgrep also helps

Say the process is running the foo command or any other command or script

pgrep foo   # print the process pid running foo
pkill foo # kill the foo process. 

Obviously you need the privilege to kill the process == you must be root the user