LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-14-2012, 01:50 PM   #16
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,199

Rep: Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136

Quote:
Originally Posted by mfirth View Post
Sorry to resurrect this again, but another possible use for "kill -0" (or more likely the system API equivalent) is to check the existence of a process before communicating with it in another way - e.g.

If service X writes its process ID into /var/run/X.pid, then you can use 'kill -0 `cat /var/run/X.pid`' to determine if the process for X is still running.

There are obviously other ways of doing it (e.g. checking for the PID in /proc, or using grep on the output of PS), but they are probably less elegant

Hope this is useful

Michael
Or you could do the same thing by looking at the exit code of "ps -p PID".

Personally, when I'm checking if a process exists, I run "output=$(ps -p PID)", check the exit code, and if it's 0 (process is running), I go on to parse $output for the actual process name to ensure it's the right process using that PID.

Otherwise you can get a false positive when a process writes out its PID, stops running, and hours later some other process happens to be assigned the same PID. Just checking the exit code of "kill -0 PID" or "ps -p PID" in this case will tell you your process is running when it's really not, it could have exited long ago and now something else has simply been assigned that same PID.
 
Old 11-14-2012, 06:18 PM   #17
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I always check the for the process name anyway, to avoid issues with erroneous pid files
Code:
ps -ef|grep procname
Gets a little more involved if you can have multiple copies running.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What to do when kill -9 PID doesn't work klauswunderlich Linux - General 7 10-11-2011 09:47 PM
kill <pid> does not do anything moskito01 Linux - General 9 08-25-2009 01:20 PM
kill -9 and respawns as new pid jago25_98 Linux - Software 3 06-18-2007 03:05 PM
kill pid ekdya Debian 3 04-18-2006 06:04 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 08:48 PM


All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration