< Previous | Contents | Next >
This is a great way to test the script to make sure everything works as expected. 1.2.2. Executing with cron
The cron utility can be used to automate the script execution. The cron daemon allows the execution of scripts, or commands, at a specified time and date.
cron is configured through entries in a crontab file. crontab files are separated into fields:
# m h dom mon dow command
• m: minute the command executes on, between 0 and 59.
• h: hour the command executes on, between 0 and 23.
• dom: day of month the command executes on.
• mon: the month the command executes on, between 1 and 12.
• dow: the day of the week the command executes on, between 0 and 7. Sunday may be specified by using 0 or 7, both values are valid.
• command: the command to execute.
To add or change entries in a crontab file the crontab -e command should be used. Also, the contents of a
crontab file can be viewed using the crontab -l command.
To execute the backup.sh script listed above using cron. Enter the following from a terminal prompt:
sudo crontab -e
Using sudo with the crontab -e command edits the root user's crontab. This is necessary if you are backing up directories only the root user has access to.
Add the following entry to the crontab file:
# m h dom mon dow command
0 0 * * * bash /usr/local/bin/backup.sh
The backup.sh script will now be executed every day at 12:00 am.
The backup.sh script will need to be copied to the /usr/local/bin/ directory in order for this entry to execute properly. The script can reside anywhere on the file system, simply change the script path appropriately.
For more in-depth crontab options see Section 1.4, “References” [p. 327].