Cron¶
Cron is a system-wide service to run tasks, so-called cronjobs, in user-specified intervals.
Crontab¶
Your cronjobs are stored in your crontab, to see your current crontab, run crontab -l. This is a table that contains all the information Cron needs to run your task:
* * * * * /path/to/your/job
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── Day of the week (0-7) (Sunday can be 0 or 7)
│ │ │ └────── Month (1-12)
│ │ └──────── Day of the month (1-31)
│ └────────── Hour (0-23)
└──────────── Minute (0-59)
An asterisk (*) means that any value is valid, so if all columns contain an asterisk, the job will be started every minute, regardless of date, time, etc.
Please note that hours are always in 24-hour format, so 10 is 10 a.m., if you want 10 p.m., you need to enter 22 in the hour column.
Aliases
There are a couple of aliases that can be used instead of the numeric definitions:
@hourly: At every full hour (i.e.: 0 * * * *).@dailyor @midnight: Every day (i.e.: 0 0 * * *).@weekly: Every week (i.e.: 0 0 * * 0).@monthly: Once a month (i.e.: 0 0 1 * *).@yearlyor@annually: Once a year (i.e.: 0 0 1 1 *).@reboot: After every reboot.
Adding, Modifying, and Deleting a Cronjob¶
Standard editor
To change your standard editor, set it in the VISUAL variable. So to use nano, add this line to your .bash_profile:
If you want to add or modify a new cronjob, you need to edit your crontab. To do this, use the command crontab -e. This will launch your standard editor and you can add a new job or modify an existing one.
To remove a cronjob, delete the line. If you want to only temporarily disable a cronjob, put a # at the beginning of the line. You can also use the # to add comments to the file.
After you’ve saved the temporary file and exited the editor, the changes will be applied.
Adding random intervals¶
It makes sense to use a random interval when executing cron jobs so that not all users on the server execute the jobs at the same time. This helps us to avoid load peaks and better utilize our servers. To add a random delay between 1 and 120 seconds use the following code:
PATH¶
cron does not parse your .bash_profile or .bashrc, so $PATH is different from your shell. You can define $PATH in the crontab. So if you want to include your ~/bin directory in $PATH, you need to insert this line before your cronjob:
Afterwards log out of your Asteroid and log back in again.
Logging¶
If you want to save your cronjob’s output to a log file, you can do so by using the > and >> operators. Please note that this also disables cron’s notification mail.
To save only the most recent output, use >:
To append the log file, use >>:
Examples¶
Special characters: /, , and -:
/is used to divide a time.,combines multiple times.-specifies a range (such as 1-5).
The job /path/to/your/job/script.pl is started 15 minutes past every full hour.
The job is started every day at 10:30 a.m.
The job is started every minute.
The job is started every five minutes.
The job is started at half past the hour between 8 a.m. and 8:30 p.m.
The job is started on weekdays (Monday to Friday) at 10:30 a.m.