One of the most common and useful jobs one may come across when working with any kind of long term project involving basic systems administration is to set up scheduled tasks to be executed automatically at a planned time. This can be achieved by using Cron, a time-based task scheduler you can find in Unix-like operating systems. It can be used for running scheduled backups, monitoring disk space, execute health checks, running system maintenance tasks and similar jobs.
Basic terminology
- A task scheduled through Cron is called a cron job.
- Cron has a program that runs in the background all the time (daemon) that is responsible for launching these cron jobs on schedule.
- The schedule resides in a configuration file named crontab.
Cron syntax
Here is an example cron job:
10 * * * * /usr/bin/php /var/www/scheduled/cron.php > /dev/null 2>&1
Parts of a cron job:
timing | command | script | output |
---|---|---|---|
10 * * * * | /usr/bin/php | /var/www/scheduled/cron.php | > /dev/null 2>&1 |
- Timing: set the minutes, hours, days, months, and weekday settings. In the example, this job is being executed every 10 minutes.
- Command: in this case, we're executing a php script, so we need to call
/usr/bin/php
. - Script: the path of the script to run.
- Output: (optional) you can write the output to a file or discard it with
> /dev/null 2>&1
.
Timing syntax
The first part of the cron job string determines how often and when the cron job is going to run. It consists of five parts:
Create a Cron job
To display the contents of the crontab file of the current user:
crontab -l
To edit the current user's cron jobs:
crontab -e
I use vi editor, as it is the most common and default text editor un Unix-like systems. If you are not familiar with its use, I'm sure a quick Google search can help you. Here is a quick simple guide for this purpose.
- Type
crontab -e
- Press <esc>
- Press
i
(for "insert") to begin editing the file. - Append your Cron command.
- Press <esc> again to exit insert mode.
- Type
wq
to save and exit the file.
Examples
sintax | description |
---|---|
* * * * * | run a cron job at every minute. |
*/5 * * * * | Run cron job at every 5th minute. For example if the time is 10:00, the next job will run at 10:05, 10:10, 10:15 and so on. |
30 * * * * | Run a cron job every hour at minute 30. For example if the time is 10:00, the next job will run at 10:30, 11:30, 12:30 and so on. |
0 */2 * * * | Run a job every 2 hours: For example if the time is now 10:00, the next job will run at 12:00. |
0 7 * * * | Run a job every day at 7am: |
0 0 * * TUE or 0 0 * * 0 | Run a job every Tuesday at 00:00 |
25 17 1 * * | Run a job at 17:25 on day 1 of the month: |
5 0 * 8 * | Run a job on a specific month at a specific time: The job will be executed at 00:05, every day of August. |
0 0 1 */6 * | Run a job at 00:00 on day-of-month 1 in every 6th month. |
Conclusion
I hope this simple guide has been useful to the reader. I personally always have to lookup the syntax every time I setup a Cron job, as it isn't very often. I usually aid my setup by using this generator and this editor to help me remember.