What is cron in Linux and how do you use it?

The words open source with many icons below

Wright Studio — Shutterstock

Linux is one of the most flexible operating systems on the planet. There is very little you can't do with Linux… even automate tasks using a simple command line tool.

The tool in question is called cron and it allows you to schedule jobs for the Linux operating system.

Also: Patch now: Serious Linux security hole uncovered

Say, for example, you've written a simple backup script to back up everything in your Documents folder. It's named backup.sh and looks something like this:

#!/bin/bash

# What you want to backup. 
backup_files="/home/$USER/Documents"

# Where you want to backup to.
dest="/backup"

# Create an archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

You save that script in /usr/local/bin and give it the proper execution permissions with the command:

sudo chmod u+x /usr/local/bin/backup.sh

Now, instead of running that backup script manually every day or week, you can use cron to make it automatic. Let me show you how.

How to create a cron job

If you don't already have your terminal window open, do so now.

The cron system has its own build-in editor for cronjobs. To open your crontab in edit mode, issue the command:

If this is the first time you've issued the crontab -e command, you'll need to select your default editor. I would suggest going with nano, as that's the easiest Linux text editor to use.

At the bottom of the file, you'll create the new cron job entry. This is where it gets a bit tricky. You see, the time/date you use comes in a very specific form. There are five entries for time and date, which are minutes (0-59), hours (0-23), day of the month (1-31), month (1-12), and day of week (0-6, although you can use Sunday, Monday, Tuesday, etc., and Sunday can be represented by 0, 7, or Sunday). Let's say you want to run your backup every Sunday at 11 pm, the crontab time/date entry would be 0 23 * * 0. If you wanted that cron job to start at 11:59 pm every Friday, the entry would be 59 23 * * 5.

The complete cron entry for a Saturday 11:59 PM run would look like this:

59 23 * * 5 /usr/local/bin/backup.sh > /dev/null 2>&1

What is the > /dev/null 2>&1 portion of the entry? Simply put, if there's any output from the script, it must be suppressed; otherwise it could cause errors. For that, we use the > to send all output to /dev/null (which a like a system trash can) and then instructs cron where to send all errors with 2>&1.

Save and close the file with Ctrl+X. Once you've saved the crontab file, the job is ready and will be run at the configured time. Before the first run of the job, you might want to test the script to make sure it completes without errors, which can be done with the command backup.sh

And that's what cron does for you and how you can easily use it to automate scripts you've written for the Linux operating system.

Source