You will find that there are many tasks that need to be carried out on a regular basis on your Linux system. For example, you may need to update a database or back up users’ data in the /home/ directory. While you could run these tasks manually, it would be more efficient (and more reliable) if you were to configure the Linux system to run them automatically for you.
One option for doing this is to use cron. The cron daemon (/usr/sbin/cron) allows you to schedule jobs that will be carried out for you on a regular schedule.
crontab File Syntax
The cron daemon is installed and activated by default on Linux systems. Once a minute it checks to see if any jobs have been defined for the current time. The cron daemon uses a file called a crontab that contains a list of jobs and when they are to be run. A crontab file exists for the entire Linux system. Each user on the system can also define their own crontab file.
Each line in a crontab file defines a single cron job. The first five fields (separated by whitespace characters—spaces or tabs) define when
the cron job should be run. They use the following syntax:
|Field Number||Field Label||Range|
|3||Day of the Month||1-31|
In a user crontab file, the subsequent fields (field six and up) specify the command and its option, if any, to be run. In a system crontab file, the sixth field specifies the user to be used to execute the command in the remaining (field seven and up) fields.
The cron daemon can run any command or shell script. However, no user interaction is available when the command or shell script is run.
The following are guidelines for configuring these fields:
- If you want a job to run every minute, hour, day, or month, type an asterisk (*) in the corresponding field.
- You can include several entries in a field in a list separated by commas.
- You can specify a range with start and end values separated by a hyphen.
- You can configure time steps with /n (where n represents the size of the step).
- You can specify months and weekdays using the first three letters of their names (for example, MON, TUE, JAN, FEB). The letters are not case sensitive. However, when you use letters, you cannot use ranges or lists.
- Numbers representing the weekdays start at 0 for Sunday and run through the entire week consecutively, with 7 representing Sunday again. For example, 3 is Wednesday and 6 is Saturday.
Defining User Jobs
Users create and maintain their own crontab files using the crontab command. The following options can be used with the crontab command:
|crontab -e||Creates or edits jobs. The vi editor is used.|
|crontab file||Replaces any existing crontab file for the current user with the specified file, assuming the file contains a list of jobs using the correct syntax.|
|crontab -l||Displays current jobs.|
|crontab -r||Deletes all jobs.|
For example, suppose you want to define a cron job that copies the contents of your user’s home directory to an external USB drive mounted in /media/USB every night at 5:05 PM. You would need to do the following:
1. Open a terminal session.
2. At the shell prompt, enter ‘crontab -e’.
# crontab -e
A blank crontab file is created for your user if this is the first time you are defining a cron job.
3. Press the I key, then enter the following:
5 17 * * * cp ~/* /media/USB
4. Press Esc, then enter ‘:wq!’.
5. You can specify which users are allowed to create cron jobs and which aren’t by creating the following two files:
- /etc/cron.allow: Users listed in this file can create cron jobs.
- /etc/cron.deny: Users who are not listed in this file can create cron jobs.
If the /etc/cron.allow file exists, it is the only file evaluated; /etc/cron.deny will be ignored in this situation. If neither of these files exists, only the root user is allowed to define user cron jobs.