9

I'm concerned that cron jobs can fail silently for an indefinite period of time on vanilla Ubuntu Desktop 12.04.1 (Precise), and no one will notice. I would like to get a notification whenever a system cron job prints some output or just fails.

I know it is possible to install a mail server (e.g. postfix), configure it for local-only delivery, set up an alias so that root's mail is delivered to my normal user account and configure a mail client to check my local mailbox.

Are there any lightweight alternatives to this solution in Ubuntu?

9 Answers 9

3

You could redirect the error output of your cronjob command to a file. Here is an example of a line in /etc/crontab:

01 3    * * *   user    /bin/command 2>> /var/log/some.file

Then at least you got a clue if errors occured. You might even write a script to notify you over notify-osd or similar tools when the file changes.

Edit:

The file /var/log/syslog reports messages from cron as well. You might want to take a look at that. To get a dedicated log-file for the cron deamon edit /etc/rsyslog.d/50-default.conf and uncomment/edit the line that says:

#cron.*             /var/log/cron.log

Don't know what you'll find there but worth the try. Report how it went.

2
  • Thanks. I'm worried about existing and future system cron jobs. So I'd rather not modify them all. That would hinder upgrades and would not cover any new cron jobs received as part of a package update. So I'm looking for a solution that would apply to all system cron jobs at once.
    – Till Ulen
    Aug 24, 2012 at 16:26
  • I'd be surprised if a solution existed outside of the usual mailer that fulfilled so many restraints
    – con-f-use
    Aug 24, 2012 at 17:08
1

"I would like to get a notification whenever a system cron job prints some output or just fails."

I'd recommend using some sort of cron monitoring tool. There are a few out there but I currently use Dead Man's Snitch (https://deadmanssnitch.com) and like it. It will alert you when a cron job doesn't check in. Like you're doing, just curl your unique snitch URL after the job and hit the url. There are a few others out there like probyapp but they aren't free...good luck.

3
  • I hope you don't mind me saying, I think you should check out cronitor.io. I built Cronitor after trying to use DMS but realizing that it had some real flaws. Basically, it can take up to 29 days to be notified that a monthly cron job failed to run. We built Cronitor with python on ubuntu, you should come check it out.
    – Shane H
    Oct 18, 2014 at 22:53
  • 3
    I've built another one: healthchecks.io, code on github Jul 11, 2015 at 20:22
  • @Peteris Awesome work
    – aklmie
    Aug 15, 2015 at 15:46
1

Jenkins is a fairly helpful tool for this sort of thing. I know people tend to think of it as a CI, but it's really just a job execution tool. It will capture the output, run time and exit code status of a job which it uses to determine whether or not the job is a failure.

You can setup ssh-agent to have Jenkins connect to a remote machine, schedule jobs to run just like crons, automatically space them based on run time (instead of "3am"), chain dependencies, rotate logs of output on a per-job basis and integrate with a bunch of outside systems (Slack, Hipchat, etc) via plugins to be notified of failures.

Last time I set this up, it was a huge help. We knew immediately if there were issues anywhere and were able to centrally control and track crons from multiple different systems.

0

I have done something like the follows, but my use case is for a script or executable set in cron :

0 1,13 * * * /usr/bin/php /var/www/html/script.php > /tmp/script.log 2>&1 ; [ $? -ne 0 ] && mail -s "Cron Failure Report `date` " [email protected] <<< "Cron script.php Failed in Execution"

hope it helps!

For system wide application, DMS is an option. Kindly update if you found a way other than this to do it.

0

On modern Ubuntu systems use

journalctl -u cron.service -x

To notify an error without sending emails, try this:

0 1,13 * * * /usr/bin/php /var/www/html/script.php > /tmp/script.log 2>&1 ; [ $? -ne 0 ] && notify-send -u critical -t 3000 "Cron script.php Failed in Execution" "Type journalctl -u cron.service -x to see more details"
1
  • Cannot autolaunch D-Bus without X11 $DISPLAY when I want to show notification.
    – Gelldur
    Nov 23, 2022 at 11:07
0

cronwrap 1.4

web: https://pypi.org/project/cronwrap/

A cron job wrapper that wraps jobs and enables better error reporting and command timeouts.

Install with:

pip install cronwrap
0

Try this (example shown; discussion here: https://unix.stackexchange.com/questions/265026/rsyncs-read-only-file-system-30-error/613594#613594):

## all as one line (broken here for readability):

0  5  *  *  *  <your_username>  nice -n 19
  rsync -aq --delete /mnt/Music/ /mnt/Music_Backups ; EXIT_STATUS=$?;
  if [ $EXIT_STATUS -ne 0 ]; then
  notify-send -i warning -t 0 "/etc/crontab for rsync'd Music Backups failed"
  --icon=dialog-information ;
  mutt -s "/etc/crontab for rsync'd Music Backups failed" <your_email_address>; fi
0

To get an email every time a cron job, you have scheduled to run, fails:

  1. Redirect all of your standard output to /dev/null or to some file.

  2. Use the MAILTO environment variable with a list (comma separated) of all the email addresses you want to receive the email notifications.

To avoid using the same value of MAILTO for every task you run, you can just write it in the crontab e.g.:

MAILTO="[email protected]"
0 5 * * * /bin/some_script > /dev/null

MAILTO="[email protected]" to the top of a crontab will cause any output from the cron job to be emailed. So we redirect the STDOUT to null and if any STDERR messages are present, they will get emailed to you.


SRC

-2

Have you tried writing script? I'm sure there is an easy way like:

if cron.output = fail then notify-send "sorry bro"

2
  • Do you have an idea how to integrate such a script with cron? Modifying all system cron jobs one by one is not a good idea. I'm looking for a solution that would apply to all system cron jobs at once.
    – Till Ulen
    Aug 24, 2012 at 16:28
  • down voted. sorry, bro.
    – Akhil
    Jan 9, 2022 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .