GuidesA Primer for Scheduling Cron Jobs in Linux

A Primer for Scheduling Cron Jobs in Linux

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




Cron jobs in Linux are simple scheduled tasks that can be set to run commands at specific times. Unfortunately, the syntax isn’t the easiest to use or remember, but in this month’s column I’ll share some examples and tips to help you better understand and utilize cron jobs.

To begin, enter this into a Terminal window to view the current logged-in user’s crontab entry:

crontab -l

Next enter the following to edit the current logged in user’s crontab entry:

crontab -e

When editing, you need to enter a task on each line beginning with values for the minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (1-7), and then the command you want the Cron job to run. You can use an asterisk (*) to specify any value, a hyphen (-) to specify a range of values, or a comma (,) to list multiple values. For instance, the following would reboot every day at midnight:

0 0 * * * reboot

Server Tutorials - Rounded

0 0,12 * * * reboot

The command below would reboot at midnight and noon on working (Mon – Fri) days:

0 0,12 * * 1-5 reboot

However, one great shortcut is to use the following keywords instead of the five time/date fields:

Keyword

Equivalent

@yearly

0 0 1 1 *

@weekly

0 0 * * 0

@daily

0 0 * * *

@hourly

0 * * * *

@reboot

Run at startup.

For instance, the following command will have the system reboot every day at midnight:

@weekly reboot

But if you need to set something with a more specific schedule, consider using the Corntab site or their iOS app to help you build the proper syntax. You can specify the minute, hour, day(s) of the month, months, day(s) of the week, and the command you wish to run, and then copy or email the crontab entry.

If you’d like to run multiple commands consecutively, you can use the double-ampersand (&&) in the “command” section, but it only proceeds with the other command if the previous command exits successfully. Here’s an example:

@weekly command && command

By default, cron saves any output from the Cron command to the user’s mailbox on the local system. You can also configure crontab to forward this information to another user’s mailbox by starting your crontab with the following line:

MAILTO=username

Or if you configure your mail server program with your SMTP information you can have all the output sent to a real email address, as in the case of the following:

MAILTO="yourname@yourdomain.com"

I should also mention that GNOME has its own scheduling application called Configure Scheduled Task (gnome-schedule), which lets you set up cron jobs with a simple GUI. It includes additional functionality as well, including saving templates for future jobs, easily suppressing output and handling X apps if you want to set an X application up as a cron job.


Eric Geier
is a freelance tech writer — keep up with his writings on Facebook. He’s also the founder of NoWiresSecurity, a cloud-based Wi-Fi security service, and On Spot Techs, an on-site computer services company.

Follow ServerWatch on Twitter and on Facebook

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories