The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

keeping an eye on cron jobs - best practices?

Discussion in 'General Discussion' started by spaceman, Apr 16, 2006.

  1. spaceman

    spaceman Well-Known Member

    Joined:
    Mar 25, 2002
    Messages:
    481
    Likes Received:
    0
    Trophy Points:
    16
    It came to our attention recently that a cron job that should have been running every day had not run successfully since last November :eek:

    My question - can anyone give any 'best practice' advice in relation to ensuring that if cron jobs

    a) don't run (at all), or
    b) run, but without a successful result

    ... then we are alerted accordingly? Of course 'successful result' will be different from one cron job to the next, which doesn't make this an easy question to answer in a general way.

    Thx.
     
  2. jackie46

    jackie46 BANNED

    Joined:
    Jul 25, 2005
    Messages:
    537
    Likes Received:
    0
    Trophy Points:
    0
    What cron job are you talking about?
     
  3. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    As far as monitoring crond, you could use a simple script and a cron job to email you daily, hourly, or whatever, and if you stop getting the emails, you know crond is down. As far as being notified if a particular cron job stops, if a job stops, it won't do anything, so you'd pretty much have to use a new cron job to monitor that one, but then if crond goes down, it won't work either.

    Complicated, but it's pretty hard to be notified when something stops without having something else watch it.
     
  4. netkinetics

    netkinetics Well-Known Member

    Joined:
    May 14, 2004
    Messages:
    62
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Home is Baltimore, MD - Currently in the Philippin
    You could do this a few ways. Depending on what you are accomplishing via cron.

    Most programs exit with 0 normally, however you'd need to verify this.

    Make a small script for each cron you'd like to run, have crontab call the script.. inside do something like this:

    #!/bin/bash
    /path/to/myprogram > /dev/null 2>&1
    if [ "$?" != 0 ]; then
    echo "Cron job (my cron name failed to exit normally! Exit code was $?" | mail -s "Cron failure notice" you@yourdomain.com
    fi

    Just make sure your program exits on 0 normally. Most GNU Linux distributions (like Debian) take care to ensure that everything exits 0 on success, 1 on failure then 2, 3, 4 etc .. for special more descriptive error codes.

    Or you could craft a small php script to receive the data, and use lynx or something similar to send it over, i.e.

    if [ "$?" != 0 ]; then
    lynx --source http://mydomain.com/cronwatch.php?cron_id=xx&result=$? > /dev/null 2>&1
    fi

    You could then keep a birds eye view on what each server is doing via cron.

    I've also found its practical to get the system load inside of these scripts to determine if it is, or is not a good idea to run the cron. I.e., a bad idea to run a backup script while loads are already high.

    so ..

    load=`cat /proc/loadavg | tr '.' ' ' | awk '{ print $1}'`
    if [ -z "$load" ] || [ "$load" -ge 4 ]; then
    sleep 300
    else
    do my cron stuff here
    fi

    Just make sure you add a & to the cron command (in the crontab) to launch this so it sleeps in the background if necessary.

    None of the above is a ready to cut and paste solution, but should point you in the right direction.

    HTH

    Tim
     
  5. spaceman

    spaceman Well-Known Member

    Joined:
    Mar 25, 2002
    Messages:
    481
    Likes Received:
    0
    Trophy Points:
    16
    Thanks everyone - lots of good stuff to get stuck into :)
     
Loading...

Share This Page