Introduction
I must admit, after a decade of working professionally with unix/linux, that I had never encountered kill -0 until last week.
What does the -0 signal do?
Nothing at all...
What is it useful for?
It returns 0 if the process exists, or 1 (and an error message on stderr) if it doesn't.
(Strictly, it returns whether a process can accept a signal, rather than whether it exists. Can anyone enlighten me on the difference? Kernel processes can accept signals - I didn't know that before I tried this - what can signaling kernel processes be used for?)
It's just a much nicer way of finding out whether a particular process id exists. Other alternatives I have seen/used include grepping through the output of ps -ef and checking for the existence of the appropriate directory in /proc.
Have a look at my entry on startup scripts for more details.
Example
chris@chris-laptop:~$ sleep 60 & [1] 1316 chris@chris-laptop:~$ kill -0 1316 # process id of the sleep command chris@chris-laptop:~$ echo $? 0 chris@chris-laptop:~$ kill -0 65535 # process does not exist bash: kill: (65535) - No such process chris@chris-laptop:~$ echo $? 1 chris@chris-laptop:~$ sudo kill -0 27835 # a kernel process chris@chris-laptop:~$ echo $? 0
Comments
Tiffany & Co Shop specialises
Tiffany & Co Shop specialises in superior sterling silver Tiffany Jewelry, Tiffany Shop provides hundreds of discount and fashion Tiffany Jewellery,
tiffany jewellery
tiffany co
tiffany
GHD Styler Hair Straightener - shop online for ghd stylers, the new limited edition purple ghd IV styler & gift set, ghd hair care products,
cheap ghd
ghd straighteners
ghd pink
ghd
ghd Hair Straighteners
the women like ugg boots,ugg cardy boots,you could buy cheap ugg boots from here!
ugg boots
uggs
Abercrombie
High Waist Straight-leg pants outfit stunted jacket, showing slender eye-catching position, pertinent mixture of units Cheap Abercrombie and Fitch Outerwears Cheap Abercrombie and Fitch Outerwears discount Abercrombie and Fitch Outerwears discount Abercrombie and Fitch Outerwears Abercrombie and Fitch Outerwears sale Abercrombie and Fitch Outerwears sale buy Abercrombie and Fitch Outerwears buy Abercrombie and Fitch Outerwears Cheap Abercrombie Fitch Outerwears Cheap Abercrombie Fitch Outerwears discount Abercrombie Fitch Outerwears discount Abercrombie Fitch Outerwears Abercrombie Fitch Outerwears sale Abercrombie Fitch Outerwears sale buy Abercrombie Fitch Outerwears buy Abercrombie Fitch Outerwears Cheap Abercrombie Outerwears Cheap Abercrombie Outerwears discount Abercrombie Outerwears discount Abercrombie Outerwears Abercrombie Outerwears sale Abercrombie Outerwears sale buy Abercrombie Outerwears at Break; DSquared jeans on The patches, holes, stains a Less.
There's a command for that.
You might be better off using the gnu program to do that, pidof.
http://en.wikipedia.org/wiki/Pidof
http://linux.die.net/man/8/pidof
There's a command for that.
Thanks, I hadn't come across pidof before.
Pidfiles typicaly use a
Pidfiles typicaly use a technique essay topics | abortion essay | marketing essay like this. Below is a bourne shell example from my backup script. The purpose is to prevent two backups from running at the same time.
narrative essay | persuasive essay
this is what pidfiles are for
Pidfiles typicaly use a technique like this. Below is a bourne shell example from my backup script. The purpose is to prevent two backups from running at the same time.
# exclusion pidfile=/tmp/backup.pid if test -f $pidfile then pid=`cat $pidfile` if kill -0 $pid then echo $0 running as $pid > STDERR exit 1 fi fi echo $$ > $pidfile"no such process" vs. "operation not permitted"
It shouldn't be a problem in most cases. Maybe it wasn't obvious, I was replying to this question in your post:
Strictly, it returns whether a process can accept a signal, rather than whether it exists. Can anyone enlighten me on the difference?
On the other hand, there might be cases where this will cause a problem. Imagine a startup script for some service that you normally start as a non-privileged user, a script that you also normally start as this very user. It is written so that it doesn't try to call `su'. And now someone accidentally starts this script as root - the service will probably seem to work fine until you try to restart it, then `kill -0' will give you an "operation not permitted" error.
The correct solution for this case is of course to change the startup script to that it tries to su to the non-privileged user if it is called as the wrong user. If it is called as root this will succeed, if it is called as another user then it will throw an error which should be noticed right away.
"no such process" vs. "operation not permitted"
Thanks, I understand now. Can you think of any problems with identifying running processes by looking at numeric directory entries in /proc?
Well, it's less portable, I
Well, it's less portable, I guess it would break on a BSDish system. My coworkers all use macs, so I should not break their development setup.
I first thought I could get a list of running processes from the OS to see if my processes are still running, but lhere seems to be no portable way to do that other than parsing the output from `ps' - which is of course fine for a shell script but looks like a hack if you're doing it in a "real" programming language. And of course you have to test your script against all different versions of `ps' that you might encounter. When you want to do this sort of thing getting the information from /proc starts to look like a really good idea.
Existing processes might not like to get signals from you...
You might get a failure if the process does not belong to you and you are not root. So kill -0 will fail, although the process exists: it just doesn't accept signals from you.
I used the same technique from a ruby startup script to determine if a previous instance was still running, so that I could delete the PID files safely otherwise.
Existing processes might not like to get signals from you...
Good point, but it shouldn't often be a concern, as most start scripts are run as root, even if some of the daemons run as less privileged users.