The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

LinkBacks (?)
LinkBack to this Thread: http://www.unix.com/unix-advanced-expert-users/79267-trick-bash-scripters-check-if-process-running.html
Posted By For Type Date
expert: Blogs, Photos, Videos and more on Technorati This thread Refback 09-01-2008 11:59 PM

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to check how long the process has been running. ukatru UNIX for Advanced & Expert Users 2 08-17-2008 05:22 PM
Check whether ftpd process is running or not? The.White.Rider SUN Solaris 8 06-17-2008 04:51 AM
check process running rose1207 Shell Programming and Scripting 4 12-27-2007 10:23 PM
script to check for a particular process and alert if its not running goks Shell Programming and Scripting 1 12-09-2005 02:11 AM
How to check if another instance of the process is running sim Shell Programming and Scripting 8 06-30-2005 04:24 AM

Reply
 
Submit Tools LinkBack (1) Thread Tools Search this Thread Display Modes
  1 links from elsewhere to this Post. Click to view. #1  
Old 08-31-2008
Registered User
 

Join Date: Aug 2008
Location: Portugal
Posts: 213
Trick for bash scripters (check if process is running)

I have seen this question -- check if a process is running -- in a lot of forums and all seem a bit complex and unnecessary:
Checking if /proc/PID exists?
ps aux | grep -E "PID|name" ?
etc...

There is a simpler way of doing it: sending signal 0 (zero) to the process and letting the kernel tell if the process is running. So all you have to do is kill -0 PID or killall -0 name and check the return value of kill command using $?

bash
Code:
kill -0 pid
echo $?
ruby
Code:
pid = ARGV[0]
begin
    Process.kill(0, Integer(pid))
    puts "#{pid} is alive!"
rescue Errno::EPERM                     # changed uid
    puts "#{pid} has escaped my control!";
rescue Errno::ESRCH
    puts "#{pid} is deceased.";      # or zombied
rescue
    puts "Odd; I couldn't check the status"
end
python
Code:
import sys
import os

try:
        os.kill(int(sys.argv[1]), 0)
        print "Running"
except:
        print "Not running"
perl
Code:
kill 0, $ARGV[0] or die "Not running";
print "Running\n";
C
Code:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

int
main (int argc, char * argv[])
{
        printf ("%s\n", !kill (atoi(argv[1]), 0) ? "Running" : "Not Running");
        return 0;
}
Any comments or programming examples?
Reply With Quote
Forum Sponsor
  #2  
Old 08-31-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
'Advanced programming in the UNIX environment' explains the reason for having this feature. I did not see where you mention 'only works for processes that your process has permissions to signal'.

And if the shell has a kill builtin, it is possible the behaviors are different from those listed in the man page for /usr/bin/kill. kill is a builtin in the bash shell and the ksh shell. To get the man page behavior for a builtin like kill - (or for example:test or echo), you need to specify the image file like:
Code:
/bin/echo
/bin/kill
/bin/test
# /bin == /usr/bin  you can use either
Reply With Quote
  #3  
Old 08-31-2008
Registered User
 

Join Date: Aug 2008
Location: Portugal
Posts: 213
Quote:
Originally Posted by jim mcnamara View Post
'Advanced programming in the UNIX environment' explains the reason for having this feature. I did not see where you mention 'only works for processes that your process has permissions to signal'.
The ruby example handles an exception for that situation: EPERM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0