Shell script to capture Process ID and kill it if exist

user1597811 picture user1597811 · Dec 17, 2012 · Viewed 221k times · Source

I tried this code and it is not working

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
Kill -9 PID
fi

It is showing a error near awk.

Any suggestions please.

Answer

Anthony Mutisya picture Anthony Mutisya · Apr 9, 2013

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

Hope it helps.