This detailed guide will explain how to use the crontab command to schedule tasks in Linux/Unix.
Apart from video games one of the great advantages of using computers is that they don’t seem to mind doing boring and repetitive tasks that we would rather not do. For monotonous tasks such as running daily backups we are better off scheduling them to run at a certain time every day, and do more interesting stuff with our time. Linux and UNIX usually come with the utility “crontab” by default. This very useful command allows you to schedule tasks to run at a specified time and repeat as configured. A task scheduled using “crontab” is called a “cron job”.
The crontab command allows you to instruct your server to execute a script at a specified minute of the specified day of the particular month. To see what cron jobs are setup to run on your machine, run the following command:
# crontab -l
If your machine has some cron jobs configured it will display something like the following:
0 6 * * * /opt/scripts/backup_script.sh
The crontab configuration above shows a cron job that executes the shell script “backup_script” everyday at 6 AM. The * means to run at every instance of the value in the field. So an asterisk in the day, month, and day of week fields in the above example means that the script should run every day of the month, every month of the year, and every day of the week. The first and second fields tell crontab to execute the script at zero minutes and six hours, which is 6 AM. So, this cron job configuration would read something like this, “Run the script /opt/scripts/backup_script.sh at zero minutes, 6 AM, every day, every month, and every day of the week.” Below is the “anatomy” of a crontab configuration explained.
0 6 * * * /opt/scripts/backup_script.sh
| | | | |
| | | | |________________ day of week (Sunday=0)
| | | |__________________ month of year
| | |____________________ day of month
| |______________________ hour of day
|________________________ minute of hour
To schedule new cron jobs or to edit old ones execute the following command:
# crontab -e
This will bring up an editor with which you can add, edit, or delete “cron jobs”. To schedule a job to run at 3:45 PM every Monday add entry like this:
45 15 * * 1 /opt/scripts/script.sh
You can also use a comma to add multiple entries into a field. This is useful if you want to run a script thrice a day, with an eight hour interval. Make sure you do not leave space between entries in a single field, though. Your “cron job” entry would look like this:
0 1,9,17 * 2,11 * /opt/scripts/db_backup_script.sh
In the above example the script “db_backup_script.sh” runs at 1 AM, 9 AM, and 5 PM every day in the months of February and November. There are a lot of cool things that you can do with cron jobs. The same works for day, month, and day of week. Once you get the hang of this try out the following. Crontab doesn’t only understand numbers, it can also operate on some basic English words:
@reboot Run once, the next time the system reboots
@yearly Run once a year, at the beginning of the year
@annually Same as @yearly, run once annually
@monthly Run once a month, at the beginning of the month
@weekly Run once a week, at the beginning of the week
@daily Run once a day, at midnight
@midnight Same as @daily, run at midnight
@hourly Run once an hour, at the beginning of the hour
So, for example, if you want to run a script at midnight every night, this is what your crontab configuration would look like:
Using “@midnight” rather than “0 0 * * *” will work the same and execute the requested script at midnight every night while being a lot more readable than its numerical counterpart. You can also make the use of hyphens to declare a range. In the command below “script.sh” will be executed at the beginning of every hour from 1 AM to 6 AM:
0 1-6 * * * /opt/scripts/script.sh
There are many more cool things you can do with cron. Every sysasmin swears by it, it’s pretty easy to use once you get the hang on it, and very powerful. Use this tool wisely, as scheduling a task wrongly can sometimes do a lot of harm. Reading the “man page” of crontab would be highly recommended. It’s also worth learning about the at command, as it’s used for scheduling commands too.