Tux

...making Linux just a little more fun!

Process monitor

Brian Sydney Jathanna [briansydney at gmail.com]
Tue, 26 Sep 2006 14:56:13 +1000

Hi,

I am facing a problem with one of my services which needs to be constantly monitored and restarted in case it dies. I was just wondering if there is a command / program / script which can be placed in crontab to monitor a process and restart it if its dead. Thanks in advance.

Brian.


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]
Tue, 26 Sep 2006 06:17:32 +0100

On Tue, 26 Sep 2006 14:56:13 +1000 "Brian Sydney Jathanna" <briansydney at gmail.com> wrote:

> Hi,
> 
> I am facing a problem with one of my services which needs to be
> constantly monitored and restarted in case it dies. I was just
> wondering if there is a command / program / script which can be
> placed in crontab to monitor a process and restart it if its dead.
> Thanks in advance.
> 
> Brian.

restartd
monit
... are a few of the programs that spring to mind.

-- Thomas Adam

-- 
"If I were a witch's hat, sitting on her head like a paraffin stove, I'd
fly away and be a bat." -- Incredible String Band.


Top    Back


Ramon van Alteren [ramon at vanalteren.nl]
Tue, 26 Sep 2006 10:26:09 +0200

Thomas Adam wrote:

> On Tue, 26 Sep 2006 14:56:13 +1000 "Brian Sydney Jathanna"
> <briansydney at gmail.com> wrote:
> >> I am facing a problem with one of my services which needs to be
>> constantly monitored and restarted in case it dies. I was just
>> wondering if there is a command / program / script which can be
>> placed in crontab to monitor a process and restart it if its dead.
>> Thanks in advance.
>>
>> Brian.
> 
> ```
> restartd
> monit
> '''
> ... are a few of the programs that spring to mind.

On a larger scale nagios can do that for you too, It allows you to define actions once one of the monitorred services goes down.

Regards,

Ramon

-- 
To be stupid and selfish and to have good health are the three 
requirements for happiness, though if stupidity is lacking, the others 
are useless.


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]
Tue, 26 Sep 2006 09:36:49 +0100

On Tue, 26 Sep 2006 10:26:09 +0200 Ramon van Alteren <ramon at vanalteren.nl> wrote:

> Thomas Adam wrote:
> > On Tue, 26 Sep 2006 14:56:13 +1000 "Brian Sydney Jathanna"
> > <briansydney at gmail.com> wrote:
> > >> I am facing a problem with one of my services which needs to be
> >> constantly monitored and restarted in case it dies. I was just
> >> wondering if there is a command / program / script which can be
> >> placed in crontab to monitor a process and restart it if its dead.
> >> Thanks in advance.
> >>
> >> Brian.
> > 
> > ```
> > restartd
> > monit
> > '''
> > ... are a few of the programs that spring to mind.
> 
> On a larger scale nagios can do that for you too,
> It allows you to define actions once one of the monitorred services
> goes down.

That might be overkill for this, I am not sure since the querent gives no indication as to which processes or how many.

Over the years, I have seen many people try various incantations of the following:

[ "$(ps aux | grep '[p]rogramName')" ] || programName &
Particularly in crontabs and the like. But that's just wrong. In situations such as that the 'daemontools' package should be used (since that's effectively the operation they're wanting to do).

Other methods of this (and this one is only slightly better) is to try something like:

#!/bin/sh
 
# Start the program
programName &
 
# Get the PID
thepid=$!
 
while sleep 60; do
  if kill -0 $mypid; then
     # Process is running...
  else
     # Process not running....
  fi
done
The above works because signal "0" doesn't exist.

-- Thomas Adam


Top    Back


Ramon van Alteren [ramon at vanalteren.nl]
Tue, 26 Sep 2006 12:52:06 +0200

Thomas Adam wrote:

> On Tue, 26 Sep 2006 10:26:09 +0200
> Ramon van Alteren <ramon at vanalteren.nl> wrote:
>> On a larger scale nagios can do that for you too,
>> It allows you to define actions once one of the monitorred services
>> goes down.
> 
> That might be overkill for this, I am not sure since the querent gives
> no indication as to which processes or how many.  
> 

Agreed.

> Over the years, I have seen many people try various incantations of the
> following:
> 
> ``
> [ "$(ps aux | grep '[p]rogramName')" ] || programName &
> ''
> 
> Particularly in crontabs and the like.  But that's just wrong.  In
> situations such as that the 'daemontools' package should be used (since
> that's effectively the operation they're wanting to do).

Agreed, however replacing the distribution provided startup/shutdown script setup with daemontools is far from trivial in most cases.

> Other methods of this (and this one is only slightly better) is to try
> something like:
> 
> ``
> #!/bin/sh
> 
> # Start the program
> programName &
> 
> # Get the PID
> thepid=$!
> 
> while sleep 60; do
>   if kill -0 $mypid; then
>      # Process is running...
>   else
>      # Process not running....
>   fi
> done
> ''
> 
> The above works because signal "0" doesn't exist.

I like this approach, this can be integrated with existing startup scripts without much problems and still monitor the service for being up.

That doesn't take into account that a lot of daemons can be running while not "being up" as perceived by a user. That would require more elaborate checking against expected behavior from the server (f.e. can I login to this POP server with my password)

Grtz Ramon

P.S> Thomas, thanks for the CC, but I'm on-list so there's no need.

-- 
To be stupid and selfish and to have good health are the three 
requirements for happiness, though if stupidity is lacking, the others 
are useless.


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]
Tue, 26 Sep 2006 12:05:46 +0100

On Tue, 26 Sep 2006 12:52:06 +0200 Ramon van Alteren <ramon at vanalteren.nl> wrote:

> Agreed, however replacing the distribution provided startup/shutdown 
> script setup with daemontools is far from trivial in most cases.

But you wouldn't do that anyway, I should hope.

> I like this approach, this can be integrated with existing startup 
> scripts without much problems and still monitor the service for being
> up.

What's with this fascination of init scripts? ;) Most daemons that are daemons (i.e. written as such) are only ever started up in this way.

> P.S> Thomas, thanks for the CC, but I'm on-list so there's no need.

You get that for free because I replied with 'send all'. Upadate your MDA to handle the Cc. I never did understand people that complained about this on mailing-lists -- it's very easily fixed. ;)

-- Thomas Adam


Top    Back