How to Set Up Cron Jobs on Linux

Scheduled Cron Jobs LinuxCron is a software utility that schedules jobs to be run, or automates tasks in the background.

Each job can be scheduled to run periodically (at specific intervals ranging from once every minute to once every year).

The cron utility is a great asset on a server that runs 24/7, when used to perform scheduled maintenance, etc.

On a PC, the cron utility can also be useful, but I personally think that it’s pranking potential is unparalleled.

Just think of the mayhem you can cause by setting up cron jobs that start a new instance of Firefox every 5 minutes, all the while checking every one minute for instances of KMahjongg (or any games) and shutting them down.

(I’m suddenly having waaaaay to much fun with this topic!)

In all seriousness, pretty much anything that can be done from the command line or a shell script can be turned into a scheduled cron job, and any repetitive tasks should be set up as cron jobs to ultimately save time and effort.

“Cron”, in this case, comes from “chronos”, the Greek word for “time”.

Cron runs every one minute, checking for scheduled tasks to be performed.

Starting & Stopping the Cron Service

I figure that I should probably lead with this (for your sanity’s sake, as my own has seriously suffered!)…

You may have to turn the cron daemon on if your Linux PC is not already set up as a server and turns on this service with each reboot.

I’m probably the only person in the history of the world that didn’t look up how to do this first, but whatever.

It started to become obvious that I needed to, when every single one of my tests were a complete failure.

But, seriously, I read through all sorts of tutorials on the subject, and only one even mentioned this, and that one at the very end.

I’m not going to put you through that. My loss of time and sanity is your gain.

To start the cron daemon (in Fedora, at least!) simply type: /sbin/service crond start

If, at any time, you want to stop the cron daemon (in Fedora, at least!) simply type: /sbin/service crond stop

Problem solved. It’s almost too easy.

The Crontab

Crontab — which stands for “cron table” — is the program used to edit and schedule cron jobs.

To open the crontab for editing, simply type crontab -e into the command line, and the crontab will open in your default command line text editor (usually VI or VIM).

Some other options for the crontab command include:

-u to append the name of the user whose crontab is to be modified.
-l to display the current crontab on standard output.
-r to remove the current crontab.
-i to remove the current crontab, but only after a ‘y/Y’ prompt for confirmation.

With these details you can experiment not only with how to open the crontab for editing, but also how to list it’s contents, etc.

Now the fun begins!

Cron Syntax

Each cron task is set up on a single line in the crontab.

That single line must, in each case, include 6 specific elements, separated by spaces:

1. MINUTE
2. HOUR
3. DAY OF MONTH
4. MONTH
5. DAY OF WEEK
6. COMMAND

The first 5 elements are the “timing elements”, where only specific values are allowed. The allowed numerical values are:

1. Minute: 0-59
2. Hour: 0-23
3. Day: 0-31
4. Month: 1-12 (January – December)
5. Day of Week: 0-6 (Sunday – Saturday)

Asterisks (*) can also be used in place of numerical values to represent all possible numbers for that element.

The 6th element, or command element, should be either the path to a command (followed by the complete command), or the path to a shell script.

Put together, a single cron job might look something like this:

0 12 * * * /path/to/command/or/script

The above example schedules the command/script to be run twice a day, every day.

* * * * * /bin/touch /home/gwen/apples-vs-oranges.txt

The above example schedules the touch command to update a file’s timestamp every one minute.

(To find the location of a command, us the which command.)

A 7th (optional) element can be inserted after the 5th timing element, and before the command element.

That optional element is the name of the user with whose permission the job will run.

* * * * * gwen /bin/touch /home/gwen/apples-vs-oranges.txt

Additional Cron Timing Element Values

There are several operators (besides the asterisk) that can be used alongside the values in cron timing elements.

1. The comma (,) specifies a list of values: 1,2,3
2. The dash (-) specifies a range of values: 15-20
3. The slash (/) specifies a step value: */4 (every 4 hours)
4. The hash (#) is allowed in days-of-the-week, followed by a number 1-5, to specify constructs such as “the 4th Tuesday” of a given month.
5. An ‘L’ specifies the last given day of the month when used in the day-of-week field: ‘4L’ = last Thursday of the month
6. A ‘W’ specifies the nearest weekday (Mon – Fri) to the day-of-month field: ‘5W’ = either the fifth day of the month or the nearest weekday to it
7. The question mark (?) can sometimes be used in place of the asterisk, and other times (in other distros) as a placeholder to run tasks at the same time/hour as cron last started.

Special strings can be used in place of the 5 timing elements, to indicate specific times or intervals for a job to be run.

@reboot will run a cron job once, at startup.
@yearly will run a cron job once a year, on January 1st.
@annually will run a cron job once a year, on January 1st.
@monthly will run a cron job once a month, on the first day of the month.
@weekly will run a cron job once a week, at the beginning of the first day of the week.
@daily will run a cron job once a day, at the beginning of each day.
@midnight will run a cron job once a day, at the beginning of each day.
@hourly will run a cron job once an hour, at the beginning of each hour.

Example: @hourly /bin/touch /home/gwen/apples-vs-oranges.txt

Conclusion

You’re probably wondering why I did not yet teach you how to set up cron job pranks.

The answer is that I very simply didn’t have time!

Try some out, and let me know in the comments how they worked.

Leave a Reply

You have to agree to the comment policy.