![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
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 | |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack (1) | 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 $? 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
Code:
import sys
import os
try:
os.kill(int(sys.argv[1]), 0)
print "Running"
except:
print "Not running"
Code:
kill 0, $ARGV[0] or die "Not running"; print "Running\n"; 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;
}
|
| Forum Sponsor | ||
|
|
|
#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 |
|
#3
|
|||
|
|||
|
The ruby example handles an exception for that situation: EPERM.
|
|||
| Google The UNIX and Linux Forums |