KItis
KItis

Reputation: 5646

How to test the script deployed with crontab

I have a script which I need to run daily at 01.00 am of every month. This is what I did with cron tab to make it happen

#housekeeping
0 1 1 * * sh /product/abc/tools/housekeeper.sh -t ABC

How can I check whether this script is running? Do I have to wait till it runs or is there a process running for this script so that I can confirm it is running?

Upvotes: 0

Views: 235

Answers (2)

mtk
mtk

Reputation: 13717

Simple thing I use is to append something to a particular file before and after the desired command, like

*/2 * * * * (echo "Starting" >> ~/monitorCron.log; <Actual operation>; echo "End" >> ~/monitorCron.log;) > ~/cron.log 2>&1

This runs every 2 minutes, and appends the strings to ~/monitorCron.log.

This is the same thing I do in programming, writing a print statement where-ever and when-ever I get a doubt.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328724

The usual approach to test crontab scripts follows these guidelines:

  1. Set the time pattern to "ten minutes from now". Wait ten minutes. Check cron's logfile and the log files of your script/app for errors.

  2. Set the desired time pattern

This way, you can make sure that the script works. If something breaks, it must be because of the time pattern.

You should also redirect all output of the script to a log file. cron will send a mail with any output that the script produces but that's rarely desired.

If you always add an entry like "${timestamp} Started" to the log, you can check at your leisure that the script worked as expected.

If you want both, then you must make your script/app write the log file itself (i.e. without output redirection in the crontab) and print any errors to stdout; this will then make cron send an email.

Upvotes: 1

Related Questions