-
Notifications
You must be signed in to change notification settings - Fork 0
Running cronjobs
This wiki details a brief explanation of what a cron job is, how to set one up and troubleshoot, and includes an example of a cron job
- The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
- Fun fact: The origin of the name cron is said to be from the Greek word for time, χρόνος (chronos)
- Cron is driven by a crontab (cron table) file, which is a configuration file that specifies shell commands to run periodically on a given schedule.
- Each line of a crontab file represents a job, and is composed of a CRON expression, followed by a shell command to execute the job you wish to run at the specified time interval.
The cronjob should be in the following format (all fields listed below are required):
MINUTE HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND
Fields:
- MINUTE = the minute of the hour (options: 0-59)
- HOUR = the hour of the day (options: 0-23)
- DAYOFMONTH = the day of the month (options: 1-31)
- MONTH = the month of the year (options: 1-12 or JAN-DEC)
- DAYOFWEEK = the day of the week (options: 0-6 or SUN-SAT)
- COMMAND = the command you wish to run
Allowable Special Characters:
-
-
signifies "every" or "any" (i.e. every day of the week)
-
- , allows you to specify multiple options (i.e. 1,3,5 could mean January,March,June)
-
-
allows you to specify multiple options (i.e. 1-5 could mean January through June)
-
- */# allows you to run a job at an interval (i.e. */5 could mean run the job every 5 hours)
Examples:
-
Run COMMAND at 8:01 pm every February 1st regardless of day of the week
1 20 1 2 * COMMAND
-
Run COMMAND every Monday through Friday at 5:00 am regardless of day of the month or month
0 5 * * 1-5 COMMAND
-
Run COMMAND only Monday and Friday at 5:00 am in January regardless of day of the month
0 5 * JAN 1,5 COMMAND
-
Run COMMAND every 5 hours regardless of day of the month, month, or day of the week
0 */5 * * * COMMAND
- crontab -l will show you all the cronjobs you have running on a certain machine
- crontab -e will let you edit the list of cronjobs you have set up (in the text editor vi)
- crontab -r will remove your existing cronjobs
- /etc/cron.allow If this file exists, it must contain your username for you to use cron jobs
- /etc/cron.deny If the cron.allow file does not exist but the /etc/cron.deny file does exist then, to use cron jobs, you must not be listed in the /etc/cron.deny file
- ">> /import/monstrum/some/text/file.txt" sending your script to a file like this will send any output from the script into that file
- MAILTO=[email protected] will have the cronjob e-mail you output from the script to the e-mail you specify
- MAILTO="" will stop the cronjob from sending you output e-mails
- >/dev/null sending your script to this command will silence all "normal" output from the script and only send you error messages
For more information and more advanced cronjob implementation tips see http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
- crontabs are machine specific, so if you set it up on leonardo, you'll need to log back into leonardo to edit it
- The HOUR field is based on a 24-hour time system (i.e. 8:00 am = 8 but 8:00 pm = 20)
- Be very careful not to use the wildcard * inappropriately, you could set it to run every minute of every hour and bring the systems down
- You may need to source your bashrc file before running
- Make sure to test your cronjobs by running during work hours first (when you can check), before setting to your desired timing interval
- Open a new file with your preferred text editor and input the commands you wish to run as a cronjob
gedit onm_crontab.txt
MAILTO=""
#run ONM dowload script every Monday through Saturday night at 1:00am
0 1 * * 1-6 /import/monstrum/ONM/scripts/pre_processing/download_v2.0.sh
#run ONM B0 calculation script every Monday through Friday night at 1:00am
0 1 * * 1-5 /import/monstrum/ONM/scripts/B0/calculate_B0_onm.sh
-
cd into the directory where you saved your crontab.txt file and install the cronjob
cd /import/monstrum/ONM/scripts/
crontab onm_crontab.txt -
If you received no output to the screen, the crontab should have been installed correctly, check this by running
crontab -l
If it worked you should see your scheduled cronjobs print to the screen
If you received an error message when you used "crontab crontab.txt", you probably made a mistake creating your crontab file. Below are some common errors and ways to fix them.
- You split your schedule/command line into more than one line. Every scheduled task must fit into a single line. Do not allow your text editor to wrap it to another line. Switch off the word wrap feature in your editor if it has one.
- You did not end your file in a new line. That is, you failed to hit the ENTER (or RETURN) key after your last command line in the crontab.txt file.
- You used a word processor to create your crontab file. If the editor you were using allows you to do things like underline text, make nice bullet points, or do anything fancy besides typing raw text, you're probably using the wrong program.