The Beginner’s Guide to Using Cron to Schedule Tasks in Linux

cron-schedule-task-featured

Cron is a robust scheduler that allows you to schedule scripts or commands and run them on a regular basis. It is the heartbeat that keeps various programs and custom scripts ticking. You schedule scripts to run in the crontab file. Although this file can be intimidating upon first view, it is something that can be fairly easy to grasp with a little practice.

In some Linux distro, like Arch Linux, cron is not installed by default as it uses systemd as a cron replacement. Also, there are various cron implementation, and each has their own way of editing/scheduling cron jobs. In this tutorial we will focus on cronie, which is the default in most Linux distro, like Ubuntu.

Viewing cron entries

Viewing your crontab allows you to view scheduled tasks that will run under your account.

cron-tab-list

You can also view the scheduled tasks for root account by using sudo.

Editing cron entries

If you’ve never set up a cron job, viewing this file will show a blank file or a file with comments. To add an entry, you’ll need to open up your crontab file using the -e option.

Crontab entries will always follow the same syntax, allowing you to inspect entries on any machine as well as create them programmatically.

The basic syntax for a crontab entry expression is:

  • m = minute between 0 and 59
  • h = hour between 0 and 23
  • d = day of the month between 1 and 31
  • mon = month between 1 and 12
  • dow = day of the week between 0 and 6. Zero is Sunday.
  • Command = the command to run.

For example, you need to run a command regularly to check if your mysql server is running well. To make the command run hourly, use the following syntax:

This means that it will run on the dot at every hour. If you just want it to run at 1.42am every day, then the command becomes:

In addition, commas can be used for multiple items (such as 1,3,5) and hyphens can be used for ranges. The special use of the asterisk in the place of a item in the expression indicates for the item to run for all values of the field. (An asterisk in the hour field would say to run for each hour from 0 – 23.) A hashtag (#) on a line indicates a comment (which means that line of code won’t be executed).

Finally, a forward slash will indicate a step value. For example, every five minutes would be entered as */5 (from the moment the crontab is saved and cron job is created) . If you were to use just the “5,” the entry would only run at the five-minute mark.

For this example:

  • 34: the minute to run between 0 and 59
  • 3: the hour, between 0 and 23
  • *: the middle two asterisks indicate every day (1 to 31) and every month (1 to 12)
  • 0: the final zero indicating Sunday

This task would run at 3:34 AM every month, every week on Sunday.

Short codes

Crontab allows you to use some short codes to make your cron entries easier to read.

  • @reboot – Run once, at startup
  • @yearly – Run once a year, “0 0 1 1 *”.</>
  • @annually – same as @yearly
  • @monthly – Run once a month, “0 0 1 * *”
  • @weekly – Run once a week, “0 0 * * 0”
  • @daily – Run once a day, “0 0 * * *”
  • @midnight – same as @daily
  • @hourly – Run once an hour, “0 * * * *”

For example:

Editing cron entries as another user

There are times when you need to add a crontab as root. For example, to renew the Let’s Encrypt SSL certificate automatically, you need to run the renewal script as root. To edit crontab as root, simply add a sudo in front of the command:

Similarly, you can schedule cron job as another user, by using the -u flag:

For example, to run a cron job as the “www-data” user, use the following command:

Reviewing cron activity

Although you are now set to run specific commands or scripts, you may want to ensure it ran. Built into cron is that once a command is run, it will email the cron owner. This can be changed with the “MAILTO” variable.

cron-mailto-variable

Adding MAILTO=youremail@yourdomain.com will send all cron job reports to a specified email. This variable can typically be found at the very top of the crontab edit screen. However, if it is not there, you can add the variable, and it will work as expected.

Multiple email addresses can be separated with a comma. If you need a different command emailed elsewhere, you can add the MAILTO command directly above the command. Commands after that mailto will be emailed to the new address. If you leave MAILTO= blank, the notifications will be sent to the owner of the cron entry.

Alternatively, you can use the redirect operator (>) to send the output where it is needed.

The above will redirect the output to “/dev/null,” which sends no email and discards the output.

If you want to ensure that a job ran and don’t want an email, you can also check the cron logs. On most systems, accessing the cron logs will require superuser permissions. The cron log can be found under “/var/log.” The cron or syslog file will display the log of executed cron entries.

cron-tab-log

Cron can be further restricted to allow specific usage, and defaults can be set up. With the addition of piping and redirection, cron entries can be set up to do all manner of things. Ultimately, cron is a tool to give you the ability to run tasks at your leisure and to ensure the routine maintenance of parts of your system on your accord.

Image credit: Calendar Pages and Clock by DepositPhotos

2 comments

  1. It is recommended that unless a job must be run at a specific time, user schedule their jobs to run at “uncommon” number of minutes (11, 27 34, etc) to avoid having cron jobs stepping all over each other.

  2. “@reboot” may or may not work on each and every Linux distro as some distros use a slightly different version of CRON. Also some alternatives to CRON may or may not recognize “@reboot”.
    See https://unix.stackexchange.com/questions/109804/crontabs-reboot-only-works-for-root

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.

Sponsored Stories