One of the main tasks of the systems admnistrator is to automate tasks that need to be accomplished on a regular basis.
cron
cron is a scheduling daemon for UNIX and Linux systems. It searches for files in the directories /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly, as well as user files in /var/spool/cron. cron wakes every minute, examining the files and executing the appropriate commands. It also examines the files /etc/cron.allow and /etc/cron.deny, if they exist.
| Directory and file locations used for cron | |
| /etc/crontab | contains the time to run various cron directories |
| /etc/cron.hourly | contains files to be run hourly |
| /etc/cron.daily | contains files to be run daily |
| /etc/cron.weekly | contains files to be run weekly |
| /etc/cron.monthly | contains files to be run monthly |
| /etc/cron.allow | lists users allowed to use crontab |
| /etc/cron.deny | lists users denied to use crontab |
cron.allow and cron.deny may not be present all on systems.
Editing a crontab file
crontab files are edited using the crontab command. You must be root to view or edit another users crontab file.
crontab -l lists the contents of the current users crontab file. crontab -l user lists the contents of the crontab file of user.
crontab -r removes the current users crontab file. crontab -r user removes the crontab file for user.
crontab -e opens the current users crontab file in vi for editing. crontab -e user opens the crontab file of user in vi for editing.
A typical line in a crontab file will resemble the following:
30 2 * * * find / -name core -exec rm {} \;
This will find and delete all core files each night at 2:30.
The fields in a crontab entry (Hint-copy and paste all but the last line of this into your crontab for a handy reference when you edit):
# minute (0-59),
# | hour (0-23),
# | | day of the month (1-31),
# | | | month of the year (1-12 or Jan-Dec),
# | | | | day of the week (0-6, 0=Sunday, or Sun-Sat).
# | | | | | commands
30 2 * * * find / -name core -exec rm {} \;
There are several ways to define values:
| , | multiple values |
| - | a range of values |
| * | all possible values |
If the first field contains 0,15,30,45 this would indicate it would run every 15 minutes. To run Monday through Wednesday, the fifth column should contain 1-3. And if the fifth column is a * it will run every day. Examples
Run the command df -h and send the output to the file /root/dfusage every Sunday Night at 2:30 am.
30 2 * * 0 df -h > /root/dfusage
No comments yet.