Using Linux Cron
There are many tasks that are done periodically on a Linux system. System cleanup, or monitoring, are common ones, as is backing up your system.
These are all candidates for being scheduled to occur automatically.
The facility to schedule jobs on your Linux system, is cron. First, make sure that cron is installed on your system, and running automatically. Just how this is done, varies depending on the version and dialect of Linux that you are running.
Once cron is installed and running, it is time to schedule some jobs. This is done with the "crontab -e" command. This command takes you into the an editor (normally vi or vim) for your cron tables, which is what cron uses to determine what is to be run and the run schedule. When you exit the editor, the running cron is updated with your changes. It is best to use the crontab editor because, If you edit the cron tables directly, it may not pick up your changes until cron is restarted, typically the next time you reboot.
The editor that is used by the crontab command, is the one identified by your EDITOR environment variable. You should be familiar with it before you try to change the cron entries. Assuming it is vi or vim, you can read the man pages for this utility with "man vim". For a vim tutorial the "vimtutor" command can be used as well.
Each line in the cron table, is a command to be run at a specific time. The exception to this is lines beginning with the pound sign (#), which are comments.
- There are basically six fields, separated by spaces.
- The first five are define when to run and are the minute, hour, day of the month, month, and day of the week. Any of these may be "*", which means "all". So, a line beginning with "* * * * *" would run each minute, hour, day, etc.
- To run at specific times, these fields would have numbers in them. This can be a single number, a list of numbers separated by a comma, or a range. A range is identified by a dash (-).
To run at the top of the hour, you would use "0" for the minute field. To run at 15 and 45 minutes after the hour, you would use "15,45", and to run each of the first five minutes of the hour, "1-5". These numbers would be put in without the quotes.
The range for minutes is 0-59, for hours is 0-23, day of the month 1-31, month 1-12, and day of the week 0-6 (with 0 being Sunday).
So, to run a task at 4:15 PM, the first and the 15th of a month, the command would be "15 16 1,15 * * command". At noon every Friday would be "0 12 * * 5 command".
The command field is everything following the day of the week entry. This can be any executable Linux command. Typically, it is a shell script, containing the commands to be executed. If it is a shell script, make sure that the permissions are set with the execute (x).
Any output will be sent via email to the user for that cron table. I usually prefer to redirect the output to a log file, instead. First, I redirect any errors to the standard output, using 2>&1. Then, I redirect and append the output to a log file, using double greater than (>>).
Put it all together, and you have an entry like:
15 16 1,15 * * command 2>&1 >> /var/log/command.log
This runs the command "command", on the first and fifteenth of every month. Errors are redirected to standard output, and standard output is appended to the file "/var/log/command.log".
This is a quick introduction to cron. If you need help creating cron scripts and scheduling them with crontab contact us at CCS Retail Systems.