How to debug cron jobs?

Hi there,

I am trying to setup a cron job to run a small script with bash. Clicking “Run manually” works. All is well.

However, when I setup to run the script every hour, nothing happens.

I see from the logs that midctl tries to run it:

CRON[707817]: (root) CMD (PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin" midclt call cronjob.run 2 true > /dev/null 2> /dev/null)

But nothing happens. Even if I put a plain echo hello > /some/path.txt in the script it won’t get executed.

I checked /var/log/middlewared and don’t see any errors either.

When I manually run the midctl command line from the cron (adding -U and -P credentials) all I get is a decimal number on stdout, but no script execution.

How can I debug this and / or what could be wrong?

This is on latest Fangtooth.

The most common problem I have seen with Cronjob failures, is that the executable PATH is not listed. Cronjobs ignore the user’s SHELL Profile, RC file or Login file and only use the OS supplied PATH and SHELL parameters.

So this would fail:

#!/bin/bash
#
myprog param >>myprog.log

While this would work:

#!/bin/bash
#
/mnt/pool/home/myprog param >>/mnt/pool/home/myprog.log

Obviously optimizations can be made:

PATH=${PATH}:/mnt/pool/home
LOGFILE=/mnt/pool/home/`basename`.log

Next most common Cronjob failure is permissions. Either execute & read permissions of the script, or it’s directory when not run as root.

If it is really not running anything, then you may need to use set -x in the beginning of the script. Then make sure you capture all it’s output:

touch /mnt/pool/home.mycron.log
mycron 2>&1 >>/mnt/pool/home.mycron.log