Editing Crontab
cron(8) is a daemon that runs commands automatically at scheduled times. If you need to automatically run a job some time in the future, or run a job periodically, you want to use cron(8).
Instructions for when to run jobs, and how to run them, are located in
crontab(5) files. User crontab files
are located in /var/cron/tabs/$USER
. However, you should not edit
these files directly. crontab files should be edited using using
crontab(1), a command that ensures that
crontab files have valid syntax.
By default, crontab uses the editor defined in the VISUAL or EDITOR environmental variables, or vi if both are undefined. If you are using the default shell ksh, you can easily change the default editor for your user.
Edit your crontab by typing crontab -e
.
Cron jobs are stored in /var/cron/tabs
If the MAILTO variable is not set, the output produced by a cronjob will be sent the user's owner. To ensure these emails are sent properly, make sure your mail server is configured properly, and that your aliases file? has been set up correctly. The emails are very important because they confirm whether or not the cronjob is running properly. Optionally, you can suppress cronjob emails.
Here is the default crontab file installed for root:
# SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin HOME=/var/log # #minute hour mday month wday [flags] command # # rotate log files every hour, if necessary 0 * * * * /usr/bin/newsyslog # send log file notifications, if necessary #1-59 * * * * /usr/bin/newsyslog -m # # do daily/weekly/monthly maintenance 30 1 * * * /bin/sh /etc/daily 30 3 * * 6 /bin/sh /etc/weekly 30 5 1 * * /bin/sh /etc/monthly #~ * * * * /usr/libexec/spamd-setup #~ * * * * -ns rpki-client -v && bgpctl reload
The first 4 lines define environment variables for cron.
The next lines contain instructions: "at these times on these dates run this command". For example:
#minute hour mday month wday [flags] command # # rotate log files every hour, if necessary 0 * * * * /usr/bin/newsyslog
There are six columns: minute, hour, day of month (mday), month, day of week (wday), and then the command.
This command says that at the 0th minute, at any hour, any day of month (mday), any month, and any day of the week (wday), run the command /usr/bin/newsyslog. In simple English, rotate logs hourly.
# do daily/weekly/monthly maintenance 30 1 * * * /bin/sh /etc/daily 30 3 * * 6 /bin/sh /etc/weekly 30 5 1 * * /bin/sh /etc/monthly
The next three lines mean the following:
- At 1:30AM every day, run the daily script
- At 3:30AM every Saturday, run the weekly script. Note: Sunday is wday 0 or 7
- On the first of the month, at 5:30AM, run the monthly script.
1,2,5,9 is a list of values. 8-11 represents the inclusive range of values from 8 to 11 (including 8 and 11). An ~ means a random value. An asterisk (*) means all possible values. */5 means every value that is divisible by 5.
See crontab(5) for the full details.