I have the following crontab set up, to automatically start a server if it crashes ('start' has no effect if it's already running):
root@www:/home/admin# crontab -l
*/10 * * * * /var/foo/live/foo25/bin/liveinstance1 start >> /dev/null 2>&1
# */10 * * * * /var/foo/live/foo25/bin/liveinstance2 start >> /dev/null 2>&1
*/10 * * * * /var/foo/live/foo25/bin/livezeoserver start >> /dev/null 2>&1
(The second line is purposely commented out, as the second instance isn't in use for now)
These commands work fine when typed individually as that user. However, when the server crashes, this cron job never seems to start it.
The cron logs show nothing untoward during the time the server was down (at least not to my novice eye:
Aug 26 09:28:01 www /USR/SBIN/CRON[27005]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Aug 26 09:30:01 www /USR/SBIN/CRON[27023]: (root) CMD (/var/foo/live/foo25/bin/livezeoserver start >> /dev/null 2>&1)
Aug 26 09:30:01 www /USR/SBIN/CRON[27026]: (root) CMD (/var/foo/live/foo25/bin/liveinstance1 start >> /dev/null 2>&1)
Aug 26 09:40:01 www /USR/SBIN/CRON[27126]: (root) CMD (/var/foo/live/foo25/bin/livezeoserver start >> /dev/null 2>&1)
Aug 26 09:40:01 www /USR/SBIN/CRON[27129]: (root) CMD (/var/foo/live/foo25/bin/liveinstance1 start >> /dev/null 2>&1)
What am I doing wrong?
I shall suggest you a different way for babysitting a failing service: psmon. It's a small daemon written in Perl, having an Apache stylish config file. It allows you to define all kind of conditions from just making sure a process is alive to restarting it if the process consumes too much RAM/CPU. Restart will happen way sooner than after a potential 10 minute delay you have with your current cron.
It can also send you e-mail/log the events it ran, so you can see how often the process gets restarted if you so want.
Ubuntu uses Upstart to control daemons. See
man 5 init
. Here is a community HowTo and a Wikipedia entry.Look at some existing files in
/etc/init.d
as models for your ownliveinstance.conf
file. Pay particular attention to the required comment section at the beginning of the files.As for why your script isn't working as a
cron
job, Khai is correct that it's usually a difference in environment. You have to make sure that you set$PATH
to include any directories that you require that are not in the default$PATH
or use fully specified directories to scripts and utilities that you use.Change line in crontab to look like this (or whatever shell you wrote script in):
Also, you need to add your path to crontab, run this script: