LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 12:50 PM   #16
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142

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, 05:18 PM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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



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 08:47 PM
kill <pid> does not do anything moskito01 Linux - General 9 08-25-2009 12:20 PM
kill -9 and respawns as new pid jago25_98 Linux - Software 3 06-18-2007 02:05 PM
kill pid ekdya Debian 3 04-18-2006 05:04 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 07:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:23 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
Open Source Consulting | Domain Registration