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...

    if (kill(pid,0))

:smiley: Sorry, I couldn't resist...

Use inittab, or <A HREF="http://cr.yp.to/daemontools.html">daemontools</A>

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 :wink:

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.