14

I'm running on a couple of different Linux distros (Fedora 11, CentOS 5 and SuSE 10.2) and often run into a problem where a new script that I want to run as a cron job works fine when called direct from the command line, but fails when called from cron due to slight variations in the PATH or other required environment variables.

Is there a way I can test run an individual script as if it's being run from cron, without having to run the entire crontab or use run-parts to run the entire cron.daily/ .hourly etc. directory ? I guess I could temporarily edit my crontab to run the script in the next couple of minutes, but I'd rather have a more standalone solution so that I don't risk messing up the crontab.

gareth_bowles
  • 8,867
  • 9
  • 33
  • 42

3 Answers3

9

From google search on path in crontab

root@pingu # cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

You basically want to set the PATH and then not have to worry about the variations. Also, the quick test is to run your script via SSH on all your boxes using the same environment variables.

Jeremy L
  • 1,030
  • 6
  • 8
  • Thanks, I hadn't twigged that a crontab is just like a script in that you can declare environment variables within the crontab. – gareth_bowles Nov 14 '09 at 02:10
8

If I'm running an individually-scheduled cron job (a separate line in crontab) I'll schedule it for every minute or every other minute as I'm testing it. Once it's tested out, I'll edit the crontab line so it runs on the frequency I desire.

As a side-note, if I want to test that cron itself is working properly, and emailing the results to the correct address (VERY important) I'll add the following line to my /etc/crontab:

 * * * * * root ls /doesnotexistfoobar

Runs every minute, and tries to do an ls on a directory that doesn't exist. Should email a failure message every minute. I comment the line out when I've got it working.

Back to your question: You probably don't want to modify the existing cron job to run often, both because there's a chance of forgetting to change it back, and because running your cron.hourly, cron.daily, or cron.whatever often could have side-effects, depending on what you've got in there. To ensure that you're running the script in the same environment that it's going to be in when called from cron, I'd recommend modifying my "ls" trick above:

Add a line to your crontab to run the script directly:

 * * * * * root /etc/cron.hourly/customscript

That way you can run it as often as you need to test it.

Schof
  • 962
  • 1
  • 6
  • 10
0

I don't know of a more direct solution, but you could create a user (say, crontest) with privs to your script, and use the user-specific crontab for the 'crontest' user to test the script(s) by editing it to have the script run in the next couple minutes.

kingfish
  • 410
  • 3
  • 11