Linux provides a method of automating a task or a set of tasks using the crontab utility. The tasks run automatically in the background on a schedule you specify. You can use crontab to automatically create backups, synchronize files, schedule updates, and more.
The backup shell script discussed in Back Up a Directory in Linux using a Shell Script can be automatically run on a schedule using crontab.
The name crontab derives from chronos, which is Greek for time and tab, which stands for table. To find out what cron jobs are running on your system for the current user, open a Terminal window and type the following at the prompt:
$ crontab –l
NOTE: The letter after the dash should be a lowercase “L.”
If you want to see what cron jobs are running for root type the following:
$ sudo crontab -l
To edit the crontab file for the current user, type the following at the prompt:
$ crontab –e
If you want to edit the crontab file for root, to enable a command to run as root, put sudo before the crontab command as follows:
$ sudo crontab –e
Editing the crontab file
If this is the first time you are creating a crontab file, a message like the following displays. Press Enter to select the default editor.
When the editor opens, you will notice a line in the file beginning with a #, which indicates a comment line in a crontab file. The abbreviations following the # indicate the values to be entered on a line to set up a cron job.
Scheduling Values Explained
There are five numerical values followed by the command to be run.
|1||m (minute)||from 0 to 59|
|2||h (hour)||from 0 to 23 (0 = midnight)|
|3||dom (day of month)||from 1 to 31|
|4||mon (month)||from 1 to 12|
|5||dow (day of week)||from 0 to 6 (0 = Sunday)|
|6||command||Command or application to execute and any necessary parameters needed for the command or application.|
When you enter an asterisk (*) for a numerical value, that means every. To illustrate, let’s take the following example:
* * * * * /bin/execute/this/script.sh
This line will execute the script.sh file:
- every minute of
- every hour of
- every day of
- every month and
- every day in the week.
Basically, this script would be executed every minute.
If you wanted to run a script performing a backup every day at midnight, you could set up the following cron job:
0 * * * * /home/lori/user_guide_backup.sh
NOTE: For more information about the backup script mentioned in the above line, see our previous post, Back Up a Directory in Linux using a Shell Script.
There are special words you can use in the minute field to specify special values. The values are listed below with their description and the equivalent set of numerical values.
|@reboot||Run once, at startup|
|@yearly||Run once a year – “0 0 1 1 *”|
|@annually||(same as @yearly)|
|@monthly||Run once a month – “0 0 1 * *”|
|@weekly||Run once a week – “0 0 * * 0”|
|@daily||Run once a day – “0 0 * * *”|
|@midnight||(same as @daily)|
|@hourly||Run once an hour – “0 * * * *”|
When using special words, put the special word in for the minute value and leave out the remaining numerical values. Therefore, using a special word, the cron job entry above becomes:
By default the output of the cron job is saved in the user’s mailbox. You can choose to send the output to a log file by changing the cron job to the following:
@daily /home/lori/user_guide_backup.sh 2>&1 > /var/log/backup_results.log
Linux can report on different levels. There’s standard output (STDOUT) and standard errors (STDERR). STDOUT is marked 1 and STDERR is marked 2. Therefore, the 2>&1 tells Linux to store STDERR in STDOUT as well, creating one stream of data for messages and errors.
To pour the stream of data into a file, we used the “>” followed by the full path to the desired filename. This creates a new file or overwrites an existing file. If you want to append data to an existing file, use “>>” followed by the filename to which you want to append the data.
The final cron job entry should look like the following:
NOTE: If you wanted to run more than one cron job, enter each job to run on separate lines. There is only one crontab file for each user. All cron jobs are entered into this file.
To save your cron job, press Ctrl + X in the editor. You will be asked if you want to save the modified buffer. Press Y to save your crontab file.
When asked where to save the crontab file, press Enter to accept the default location and name.
We added a bit of code to the script discussed in the Back Up a Directory in Linux using a Shell Script post, as pictured below.
The echo command prints the text in the quotes to the results file and the date command following each echo command prints the current date with the time to the results file. Adding these lines gives you an idea of how long it took your backup to run.
In our case, it didn’t take long at all because we were not backing up many files for our example.
If you decide you don’t want any cron jobs to run for the current user, open a Terminal window and type the following at the prompt:
$ crontab –r
If you are removing the crontab file for root, enter the following:
$ sudo crontab –r
NOTE: Be sure you want to remove all cron jobs. Remember all your cron jobs are in one crontab file. If you remove this file, all cron jobs are deleted.
by Lori Kaufman