Trick for bash scripters (check if process is running) | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Trick for bash scripters (check if process is running)

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-31-2008
redoubtable redoubtable is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 7 December 2010, 12:28 PM EST
Location: Portugal
Posts: 244
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old 08-31-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 21 July 2014, 10:12 PM EDT
Location: NM
Posts: 10,149
Thanks: 267
Thanked 780 Times in 730 Posts
'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

Sponsored Links
    #3  
Old 08-31-2008
redoubtable redoubtable is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 7 December 2010, 12:28 PM EST
Location: Portugal
Posts: 244
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need to check if the process is running shifahim Shell Programming and Scripting 8 08-18-2011 10:17 PM
How do I check if a process is running in C cprogdude Programming 1 10-12-2010 05:01 AM
How to check if process is running? ladyAnne UNIX and Linux Applications 5 05-01-2010 01:20 AM
Check if Process is running Raynon Shell Programming and Scripting 3 01-08-2010 02:03 AM
check process running rose1207 Shell Programming and Scripting 4 12-28-2007 12:23 AM



All times are GMT -4. The time now is 03:19 AM.