Scheduling Tasks: Cronjobs & How to Use Crontab

Scheduling Tasks: Cronjobs & How to Use Crontab



Recently with the wind-down over the Christmas & New Year period, we seem to have had an influx of customers with self-managed servers using their time off to catch up on overdue sys admin duties. Tasks such as running updates/patches, checking over error logs, backups and security and generally improving sys admin skills. This has included a handful of customers enquiring about ‘Cronjobs’, in particular for making sure some extra tasks such as backups are covered!

Automating repetitive tasks with a little scripting is something all system admins regularly try to achieve. Creating a script to perform a command or task and a ‘Cronjob’ to run the task at specific intervals is often key to freeing up your valuable time. This might could be anything from deleting files older than a specific date for cleanup or creating a MySQL backup and transferring it to a remote server for redundancy.

The sky is the limit scripting wise. However, in this blog I’ll just concentrate on giving a crash course in using ‘Crontab’ on a Linux system from basic to advanced scheduling of tasks.

Unix/Linux operating systems feature a command ‘crontab’ (Cron derives from chronos, Greek for time and tab derives from table — time table), which allows the user to schedule commands to be performed under a specific time table.

In order to view a list of your current cronjobs, login to your server via SSH and run:

crontab -l

To edit your crontab file for current logged in user eg. to add or edit new cronjobs, run:

crontab -e

This will load your crontab file using your default text editor and allow you to make changes.

To edit the crontab file for specific user:

crontab -u username -l

Crontab Schedule Formatting Explained

Cronjobs are written in the following format:

* * * * * /root/scripts/myscript.sh

There are basically two parts to a Cronjob:

1) the 5 stars which represent time & date parts in the following order:

* minute (0 to 59)
* hour (from 0 to 23)
* day of month (from 1 to 31)
* month (from 1 to 12)
* day of week (from 0 to 6, Sunday = 0)

2) the task to run on this recurring schedule eg. a script or command.

Note: you should always use the absolute path!

Now, for the first part, if you leave a star/asterisk, it means ALL.

The Cronjob above with 5 stars, would run the script “/root/scripts/myscript.sh” every minute of every hour of every day of every week of every month, ie. every minute, without exception.

Once you have that understanding, you can cater 5 stars to run your script on a specific schedule. Here are some more advanced examples:

Execute every Sunday at 1AM
0 1 * * 0 /root/scripts/myscript.sh

Execute every Friday at 6:45PM
45 18 * * 5 /root/scripts/myscript.sh

Execute every Weekday at 6PM
0 18 * * 1-5 /root/scripts/myscript.sh

Note: you can use a “-” for a range of values eg. 1-5 will equal 1,2,3,4,5 to achieve Monday to Friday scheduling.

Execute every hour during Business hours every Weekday
0 9-17 * * 1-5 /root/scripts/myscript.sh

Execute at 1AM, 9AM, 12PM, 2PM, 6PM, 9PM on Monday, Thursday, Friday, Sunday
0 1,9,12,14,18,21 * * 0,1,4,5 /root/scripts/myscript.sh

Note: you can use a “,” to separate values eg. to achieve multiple or random schedules.

Execute twice daily at 12 Noon & 12 Midnight every Day
0 0,12 * * * /root/scripts/myscript.sh

Execute every 4 Hours
0 */4 * * * /root/scripts/myscript.sh

Note: you can use the format */x to run every x

Execute every 10 Minutes
*/10 * * * * /root/scripts/myscript.sh

That should give you an overview of how to use and of course the possibilities. The only difficulty is generally for strange schedules, such as the last day of every month — given that the months are uneven — you would need a separate script to calculate the months & their number of days to achieve this, or have it built into your script — but this is a very specific example.

Special Words

You can also use special keywords in place of the 5 stars:

  • @reboot – run once at startup
  • @yearly – run once a year (0 0 1 1 *)
  • @annually – as above
  • @monthly – run once a month (0 0 1 * *)
  • @weekly – run once a week (0 0 * * 0)
  • @daily – run once a day (0 0 * * *)
  • @midnight – as above
  • @hourly – run once an hour

For example:
@daily /root/scripts/myscript.sh

Tips N Tricks:

Crontab Output
By default crontab saves the output of the command that is run in the user’s mailbox. You may want to output to a specific logfile:

0 0 * * * /root/scripts/myscript.sh >> /var/log/cron_logs/myscript.log 2>&1

Note: Linux reports on different levels. 1 = Standard Output (STDOUT) and 2 = Standard Error (STDERR). The above statement tells Linux to combine STDERR in STDOUT, creating a single stream for messages and errors which we can send wherever!

Alternatively, to avoid filling up inboxes with output, we can pipe the output into the blackhole.

Trashing crontab output for a cronjob:
0 0 * * * /root/scripts/myscript.sh > /dev/null 2>&1



Categories

  • Abhishek Choudhary

    Nice article. I used this example to test cronjob setup over Cloud.
    I tested in Heorku and Jelastic, both worked well.
    In jelastic the way was bit easy-
    http://jelastic.com/docs/cron-job