Reputation: 1611
I have a daemon running on my server. It has been running smoothly for a while, and I keep logging every occurrence of an event in the daemon.
Yesterday I noticed something strange. The daemon had stopped running, but there was no error entry in the logs.
It is very important for my system that this daemon keeps running and that if it is stopped for any reason, it is reinitiated.
Is there a way in which I can detect at regular intervals if a particular process is running or not in Ubuntu?? If I can detect that I can easily reinitiate it, but the detection is the major problem
Upvotes: 3
Views: 2480
Reputation: 200233
If the program doesn't autmatically detach from the console (i.e. keeps running in the foreground), you could do something like this:
while /bin/true; do
/PATH/TO/YOUR/daemon
logger -p local0.warn "daemon crashed"
done
This will log a warning and respawn the daemon process. Otherwise you'll probably have to run a watchdog:
PID=`cat /var/run/daemon.pid`
if [ -z `ps hp $PID` ]; then
logger -p local0.warn "daemon crashed"
fi
or
if [ -z `ps ax | grep [d]aemon` ]; then
logger -p local0.warn "daemon crashed"
fi
either in a loop as in the first example, or via cron as suggested by Loopo.
The square brackets around the first letter of the daemon name in the last example prevent the grep process from showing in the output.
Upvotes: 2
Reputation: 2195
use cron.
let it run a script every x minutes
in your script use a line something like
ps aux | grep -c <your process/daemon name>
and check the output.
this will always return at least one (the process of checking for the process itself) so maybe if your count is greater than 1 you could assume your process is running and do nothing, otherwise restart the daemon.
https://help.ubuntu.com/community/CronHowto
if your process needs to be run by a particular user (e.g. wwwrun) make sure you start the process as that user
Upvotes: 3