Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
2  
Try piping the output to a temp log file, rather than /dev/null, to see if that gives any hints as to what's going on. –  Dentrasi Aug 26 '10 at 10:05
    
Usually when that happens to me it's because of environment differences between my regular env and the one the process runs on when it's run by cron... you should check that too... –  Khai Aug 26 '10 at 11:09

3 Answers 3

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.

share|improve this answer

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

share|improve this answer

Change line in crontab to look like this (or whatever shell you wrote script in):

*/10 * * * * /bin/bash /var/foo/live/foo25/bin/liveinstance1 start >> /dev/null 2>&1

Also, you need to add your path to crontab, run this script:

#!/bin/bash
#
# Date: August 22, 2013
# Author: Steve Stonebraker
# File: add_current_shell_and_path_to_crontab.sh
# Description: Add current user's shell and path to crontab
# Source: http://brakertech.com/add-current-path-to-crontab
# Github: https://github.com/ssstonebraker/braker-scripts/blob/master/working-scripts/add_current_shell_and_path_to_crontab.sh

# function that is called when the script exits (cleans up our tmp.cron file)
function finish { [ -e "tmp.cron" ] && rm tmp.cron; }

#whenver the script exits call the function "finish"
trap finish EXIT

########################################
# pretty printing functions
function print_status { echo -e "\x1B[01;34m[*]\x1B[0m $1"; }
function print_good { echo -e "\x1B[01;32m[*]\x1B[0m $1"; }
function print_error { echo -e "\x1B[01;31m[*]\x1B[0m $1"; }
function print_notification { echo -e "\x1B[01;33m[*]\x1B[0m $1"; }
function printline { 
  hr=-------------------------------------------------------------------------------------------------------------------------------
  printf '%s\n' "${hr:0:${COLUMNS:-$(tput cols)}}"
}
####################################
# print message and exit program
function die { print_error "$1"; exit 1; }

####################################
# user must have at least one job in their crontab
function require_gt1_user_crontab_job {
        crontab -l &> /dev/null
        [ $? -ne 0 ] && die "Script requires you have at least one user crontab job!"
}


####################################
# Add current shell and path to user's crontab
function add_shell_path_to_crontab {
    #print info about what's being added
    print_notification "Current SHELL: ${SHELL}"
    print_notification "Current PATH: ${PATH}"

    #Add current shell and path to crontab
    print_status "Adding current SHELL and PATH to crontab \nold crontab:"

    printline; crontab -l; printline

    #keep old comments but start new crontab file
    crontab -l | grep "^#" > tmp.cron

    #Add our current shell and path to the new crontab file
    echo -e "SHELL=${SHELL}\nPATH=${PATH}\n" >> tmp.cron 

    #Add old crontab entries but ignore comments or any shell or path statements
    crontab -l | grep -v "^#" | grep -v "SHELL" | grep -v "PATH" >> tmp.cron

    #load up the new crontab we just created
    crontab tmp.cron

    #Display new crontab
    print_good "New crontab:"
    printline; crontab -l; printline
}

require_gt1_user_crontab_job
add_shell_path_to_crontab
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.