Stopping A process

Hi

I want to stop a process using a shell script. how do i do that?

ie, to simulate ps -ef|grep Process name
get the process id and kill -9 process id

plz help...

How are you going to identify the process to kill?

it will be input to the script

Killing a process based on the process name can be tricky.
The process name might be a substring of another process.
And it could exist more than once.

This should work:

PID=`ps -ef | grep -w ${PROCNAME} | awk '{print $2}'`
kill -9 $PID

This will kill all occurences of the PROCNAME.

I have come across the fact that the process name you want to kill is actually a process with some arguments, like "httpd -f".
In this case it can't hurt to use quotes:

PID=`ps -ef | grep -w "${PROCNAME}" | awk '{print $2}'`
kill -9 $PID

Would you like to only kill the last occurence of the PROCNAME you may use:

PID=`ps -ef | grep -w "${PROCNAME}" | awk 'END {print $2}'`
kill -9 $PID

Perhaps others have better suggestions as I am still learning too :slight_smile:

it is not wiser to kill the process by -9 option

please read the following the article, which might help you out
When should I use kill -9?