![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| script to monitor process running on server and posting a mail if any process is dead | pradeepmacha | Shell Programming and Scripting | 13 | 03-06-2009 07:33 AM |
| UID & GID of the running process | ankurjain | UNIX for Dummies Questions & Answers | 2 | 01-09-2008 10:33 AM |
| How to create a dummy process of a process already running? | shambhu | UNIX for Advanced & Expert Users | 3 | 08-31-2007 11:22 AM |
| launch & monitor process on remote host | mrx | Shell Programming and Scripting | 1 | 10-15-2004 11:20 AM |
| Lightwight Process monitor | LivinFree | Shell Programming and Scripting | 7 | 11-21-2001 05:56 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to monitor if a process is running
I would like to know if i can monitor if a process is running.
I have one program wich is running all the time, called oliba, but sometimes it goes down, and I have to launch it again. Is there a way to monitor the pid of the program, and if the program goes down, to lauch it again? Can you give me an example of a source? Thanks in advance. |
|
|||||
|
See this thread for a discussion about doing this from a shell. But the same technique works well in C, just invoke the kill system call using zero as the signal. Then check to see if it worked. Sample source code, huh? Well... Code:
if (kill(pid,0)) Sorry, I couldn't resist...
|
|
||||
|
While this response doesn't really belong in the C Programming section, the best way that I can see to ensure that your process runs all the time would be to write some sort of wrapper shell script to run the program with. Here is how you could do it in perl:
<hr noshade> #!/usr/bin/perl use strict; my $OLIBA_CMD = '/[path to oliba here]/oliba'; my $SLEEP_TIME = 5; # check to see if it is running every 5 seconds while(1) { my @pid_array = `ps -ef | grep [o]liba`; # thanks Perderabo ![]() my $pid_count = scalar(@pid_array); if ($pid_count == 0) { # uh oh it isn't running system "$OLIBA_CMD"; # run it } elsif ($pid_count > 1) { # There is more than one instance running? # code to mail the admin or something in here } sleep $SLEEP_TIME; } <hr noshade> Hope this helps. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|