tags:

views:

126

answers:

4

Hi All,

In a bash script i want to do the following (in pseudo-code):

if [ a process exists with $PID ]; then

    kill $PID 

fi

What's the appropriate bash for the conditional statement?

Thanks

+6  A: 

I think that is a bad solution, that opens up for race conditions. What if the process dies between your test and your call to kill? Then kill will fail. So why not just try the kill in all cases, and check its return value to find out how it went?

unwind
+1 unfortunately kill(1)'s exit code doesn't distinguish the different error situations (looks like it increments the exit value by one for each process it failed to signal). if the OP doesn't mind writing their own kill(2) wrapper, he could have it exit with different values based on the value of ERRNO after a failed kill(2) call.
just somebody
at the moment I am just doing kill -9 with no check - i just get an error "process doesn't exist" if it doesn't exist which isn't very tidy. How would I test what happened?
Richard
Don't carelessly `kill -9`. That just instantly kills the process giving it no chance to clean up after itself.Instead use `kill` which is equivalent to `kill -15`.If that doesn't work, you should find out why, and only as a last resort use `kill -9`.
Christoffer Hammarström
A: 

ps(1) command with -p $PID can do this:

$ ps -p 3531
  PID TTY          TIME CMD
 3531 ?        00:03:07 emacs
oherrala
+4  A: 

To check for the existence of a process, use

kill -0 $PID

But just as @unwind said, if you're going to kill it anyway, just

kill $PID

or you will have a race condition.

If you want to ignore the text output of kill and do something based on the exit code, you can

if ! kill $PID > /dev/null 2>&1; then
    echo "Could not send SIGTERM to process $PID" >&2
fi
Christoffer Hammarström
looking at the man page, kill -0 is: "exit code indicates if a signal may be sent". So does this actualy kill a process, or just tell you if it can be killed?
Richard
`kill` is somewhat misnamed in that it doesn't necessarily kill the process. It just sends the process a signal.`kill $PID` is equivalent to `kill -15 $PID`, which sends signal 15, SIGTERM to the process, which is an instruction to terminate.There isn't a signal 0, that's a special value telling `kill` to just check if a signal could be sent to the process, which is for most purposes more or less equivalent to checking if it exists.See http://linux.die.net/man/2/kill and http://linux.die.net/man/7/signal
Christoffer Hammarström
A: 

You have two ways:

Lets start by looking for a specific application in my laptop:

[root@pinky:~]# ps fax | grep mozilla
 3358 ?        S      0:00  \_ /bin/sh /usr/lib/firefox-3.5/run-mozilla.sh /usr/lib/firefox-3.5/firefox
16198 pts/2    S+     0:00              \_ grep mozilla

All examples now will look for PID 3358.

First way: Run "ps aux" and grep for the PID in the second column. In this example I look for firefox, and then for it's PID:

[root@pinky:~]# ps aux | awk '{print $2 }' | grep 3358
3358

So your code will be:

if [ ps aux | awk '{print $2 }' | grep -q $PID 2> /dev/null ]; then
    kill $PID 
fi

Second way: Just look for something in the /proc/$PID directory. I am using "exe" in this example, but you can use anything else.

[root@pinky:~]# ls -l /proc/3358/exe 
lrwxrwxrwx. 1 elcuco elcuco 0 2010-06-15 12:33 /proc/3358/exe -> /bin/bash

So your code will be:

if [ -f /proc/$PID/exe ]; then
    kill $PID 
fi

BTW: whats wrong with kill -9 $PID || true ?

elcuco