|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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"
endpython 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
|
|||
|
|||
|
'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
|
|||
|
|||
|
The ruby example handles an exception for that situation: EPERM.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|