Operating System - Linux
1753259 Members
5952 Online
108792 Solutions
New Discussion юеВ

Killing a process within a script

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Killing a process within a script

Hi everyone,

What would be the easiest way to kill a process within a script? I'm wanting to perform a ps on something and then kill that process through automation. Thanks for your help.
10 REPLIES 10
paolo barila
Valued Contributor

Re: Killing a process within a script

Hi,
I would do something like

# kill -9 `ps -ef | grep PROCESSNAME | grep -v grep | awk '{print $2}'`

Pablo
share share share
Hein van den Heuvel
Honored Contributor
Solution

Re: Killing a process within a script


For the simple opne-liners as per above, I prefer a trivial regexpr versus the 'grep -v grep'

Something like: ps -ef | grep [P]ROCESSNAME

Here the grepped string no longer matches itself as [P] really is just P.

And if one goes into awk anyway, why not have it do it all? It gives the oppurtunity for additional tests, log messages, whatever...

To pick up a variabled (processname?) into awk, check the manpages. Notably: -v and x=y and the ENVIRON[] built-in array.

ps -ef | awk '/PROCESSNAME/ { system ("kill -9 " $2)}'

fwiw,
Hein.

James R. Ferguson
Acclaimed Contributor

Re: Killing a process within a script

Hi:

First, make usre that you isolate the process that you *really* want. Use the XPG4 (UNIX95) options of 'ps' achieves this.

Never kill with 'kill -9' except as a last resort. A 'kill -9' cannot be ignored or trapped and it doesn't give a process any chance to clean up shared memory segments or remove temporary files.

Instead, use a multi-level kill, starting with a hangup; then a simple 'kill -15'; and as a last resort a 'kill -9'.

The following code accomplishs the above. If any level of killing succeeds, the script exits.

# cat .killer
#!/usr/bin/sh
myproc=`basename ${1}`
mypid=`UNIX95= ps -C ${myproc} -o pid=`
if [ ! -z "${mypid}" ]; then
kill -1 ${mypid} 2>/dev/null || exit 0
sleep 3
kill -15 ${mypid} 2>/dev/null || exit 0
sleep 3
kill -9 ${mypid} 2>/dev/null
fi
exit 0

You can see how this works by doing:

# sleep 120 &
# ./killer sleep

(or)

# nohup /usr/bin/sleep 120 &
# ./killer sleep

If you wish to integrate this into a subroutine in an existing script, change the 'exit' to a 'return' within the subroutine.

Regards!

...JRF...
paolo barila
Valued Contributor

Re: Killing a process within a script

..and if I have more instances of a process with different parameters (let's say java) and I want kill one and only one I would use:

# ps -efx | grep/awk... #(>= hp-ux 11.11)
share share share
James R. Ferguson
Acclaimed Contributor

Re: Killing a process within a script

Hi (again):

If there is any chance that you have multiple processes by the same name, the version below accomodates that. The first version could potentially fail to kill all of the processes under certain circumstances. The same comments apply as before, otherwise.

# cat .killer
#!/usr/bin/sh
myproc=`basename ${1}`
[ -z "${1}" ] && { echo "no process specified!"; exit 1; }
mypid=`UNIX95= ps -C ${myproc} -o pid=`
if [ ! -z "${mypid}" ]; then
kill -1 ${mypid} 2>/dev/null
sleep 3
kill -15 ${mypid} 2>/dev/null
sleep 3
kill -9 ${mypid} 2>/dev/null
fi
exit 0

Regards!

...JRF...

ENOCOFFEE ;-)
paolo barila
Valued Contributor

Re: Killing a process within a script

OK Italian coffee for you all :-)
I do appreciate James' script,
I'm sorry for my sudden "kill -9", I was really rude.

I still have a point:
Let's suppose you have 2 processes:

# sleep 1000 &
# sleep 1001 &

and you wanna stop *only* "sleep 1000"

(java processes happen to have long parameters and I find useful the "ps -x" trick)
share share share
Arturo Galbiati
Esteemed Contributor

Re: Killing a process within a script

Ciao Paolo,
in this case use:
kill $(ps -fxu |awk '/[s]leep 100/ {print $2}')

trick is -x option to be able to see by PS the while command string so you will be abel to check command and parameter as well.

HTH,
Art
rmueller58
Valued Contributor

Re: Killing a process within a script

ps -ef |grep -v -f /usr/local/bin/patt.1 |grep $1 > temp.1
sudo -u root kill -9 `awk '{print $2}' temp.1`
rm temp.1
TheJuiceman
Super Advisor

Re: Killing a process within a script

Thank you all for the GREAT help!!! I'm slowing getting better at this stuff. At least I'm getting where I know what questions to ask now LOL.