Test run cron entry Test run cron entry unix unix

Test run cron entry


When I want to test my cron jobs I usually set the interval very low and monitor the logs closely. When I am convinced the entry is correct, I set the interval back to a sane value.

For example, run job every two minutes:

*/2 * * * * echo "Hello World"

And the I run tail -f on my log file (/var/log/syslogon debian).


This question has also been asked on serverfault and has garnered a couple additional answers

The following is a paraphrased version of Marco's solution:(Not sure if best ediquite is not providing a link only answer or not copying someone else's solution)

Create a environment file with a temporary cron entry

* * * * *  /usr/bin/env > /home/username/cron-env

Then create a shell script called run-as-cron which executes the command using that environment.

#!/bin/sh. "$1"exec /usr/bin/env -i "$SHELL" -c ". $1; $2"

Give it execute permission

chmod +x run-as-cron

and then it is then used like this:

./run-as-cron <cron-environment> <command>

e.g.

./run-as-cron /home/username/cron-env 'echo $PATH'


Joshua's answer does not work for me. Two problems:

  • Variables in cron-env file are not exported (set -a needed).

  • Script is still tied to current tty (setsid needed).

The script run-as-cron should be

#!/bin/sh. "$1"exec setsid /usr/bin/env -i "$SHELL" -c "set -a; . $1; $2" </dev/null

Not enough rep' to fix his answer or add a comment...