There are several reasons why you might need to schedule or automate your scripts on a personal machine or a cluster:
- You’re waiting for a job to finish before submitting another
- You’d like to automate regular backups or cleanups of your data (e.g., move new data to another location or remove unnecessary output files)
- You need to submit jobs to get around node limitations (e.g., you’d like to spread out the submissions over several days)
- You need to retrieve regularly updated data (e.g., you have a model that uses daily precipitation data and you’d like to automatically collect them every day)
Cron is a utility program on Unix operating systems that allows you to schedule or repeat such tasks in the future. There’s a crontab file associated with every user in a cluster, where you’ll input all the information needed to schedule and automate your tasks. Note that not all clusters automatically allow their users to run cron jobs[1], for example, I can use it on the Reed Group’s Cube cluster, but not on XSEDE’s Comet.
To edit the crontab file associated with your user, type the following in your command line:
crontab -e
This will open a text editor (like Vim) which you can edit. To simply view your current crontab without editing, run:
crontab -l
Crontab syntax is made up of two parts: the timer indicating when to run and the command to run:
The timer accepts five fields, indicating the time and day for the command to run:
- Minute — minute of the hour, from 0 to 59
- Hour — hour of the day, from 0 to 23
- Day of the month — day of the month, from 1 to 31
- Month — month of the year, from 1 to 12
- Day of the week — day of the week, from 0 to 7
For example the following would execute script.sh on January 2nd at 9:00AM:
0 9 2 1 * /home/user/scripts/script.sh
Special characters are naturally very useful here, as they allow multiple execution times or ranges:
Asterisk (*) — to use all scheduling parameters in a field, for example, run the script, every day at midnight:
0 0 * * * /home/user/scripts/script.sh
Comma (,) — to use more than one scheduling parameter in a field, for example, run the script every day at midnight and 12PM:
0 0,12 * * * /home/user/scripts/script.sh
Slash (/) — to create predetermined time intervals, for example, run the script every four hours:
0 */4 * * * /home/user/scripts/script.sh
Hyphen (-) — to determine a range of values in a field, for example, run the script every minute during the first 10 minutes of every hour, every day
0-10 * * * * /home/user/scripts/script.sh
Hyphens and slashes can be combined, for example, to run a script every 5 minutes during the first 30 minutes of every hour, every day:
0-30/5 * * * * /home/user/scripts/script.sh
Last (L) — this character can only be used in the day-of-the-month and day-of-the-week fields to specify the last occurrence of something, for example the last day of the month (which could differ):
0 9 L * * /home/user/scripts/script.sh
or, to specify constructs such as “the last Friday” of a every month:
0 9 * * 5L /home/user/scripts/script.sh
Weekday ( W) — this character is only allowed on the day-of-month field and is used to determine the closest weekday to that day of the month. For instance, using “15W” indicates to cron to run the script on the nearest weekday to the 15th day of the month. If the 15th is a Saturday, the script will be executed on Friday the 14th. If the 15th is a Sunday, the script will be executed on Monday the 16th. If the 15th is a weekday, the script will be executed on the same day:
0 0 15W * * /home/user/scripts/script.sh
Hash (#) — this character is only allowed in the day-of-week field and is used to specify constructs such as the second Friday of every month:
0 0 * * 5#2 /home/user/scripts/script.sh
Lastly, if you’d like to be notified whenever a script is executed you can use the MAILTO
parameter, with your email address.
The important thing to remember when running cron on a cluster (as opposed to your own machine) is that it will launch a shell that with a new clean environment (i.e., without the environment variables that are automatically applied when you log on an interactive shell) and it will likely not be able to recognize some commands or where your modules are. This can be easily addressed by sourcing your bash_rc
or bash_profile
from your home directory before running anything. You also need to remember that it will launch at your home directory and you need to specify the absolute path of the scripts to be executed, or change directory before executing them.
For example my crontab file on the Reed Group cluster looks like this:
#!/bin/bash
MAILTO=myemail@cornell.edu
00 10 * * * . $HOME/.bashrc; cd /directory/where/my/project/is; git pull; sbatch ./script.sh
30 10 * * * . $HOME/.bashrc; cd /directory/where/my/project/is; git add . ; git commit -m 'fetched data'; git push
This does the following:
Every day at 10am it sources my bashrc
profile so it knows all my environment variables. It changes to the directory of my project and pulls from git any new updates to that project. It then submits a script using sbatch
. I get an email at the same time, with the text that would that would have appeared in my command line had I executed these commands in an interactive node (i.e., the git information and a line saying Submitted batch job xxxxx
).
Then, every day at 10:30 am, I commit and push the new data back to git.
[1] If you’re just a regular user on a cluster you might need to request to be granted access. If you have root privileges (say, on a personal machine), you need to edit your cron allow and deny files:
/etc/cron.allow
/etc/cron.deny