How to Use the cron Command in Linux

Tired of running tasks manually? Use the cron command in Linux.

Cron, also known as a cron job, is a command-line utility used in Linux to schedule jobs to run at a specific time, date, or interval. It executes scheduled commands or shell scripts in the background with the help of a daemon, even when the admin or user is away from the computer. 

It is tedious to manually run tasks every now and then. With that, you can use cron to schedule repetitive tasks to run periodically. Cron also automates system maintenance and backups using a crontab/cron table, a configuration file where the shell commands are written. 

In this article, we’ll learn how to schedule tasks through the cron command. 

Let’s begin!

Cron Access Permissions.

Ensuring that only specific users can retrieve cron files prevents misuse of system resources and only an admin can do this. 

You can edit permissions by accessing the /etc/cron.allow and the /etc/cron.deny files. Add the user’s name in the former to allow them to use cron jobs, or the latter to restrict them. 

If both files don’t exist in your system, depending on the configurations, either all users can use cron jobs or only the admin can.

Using crontab.

To start using crontab, open your Command Terminal by pressing Ctrl + Alt + T. This is where you will type your commands and schedule jobs. 

Here are some of the commands used: 

$ crontab –e

This command is used to create and edit cron files. If this is the first time the command is run, you will be prompted to choose an editor.

cron command in Linux

When you are done doing so, you will see a screen that allows you to edit and schedule a job.

cron command in linux

This command displays all the existing cron jobs.

$ crontab -l
cron command in linux

To delete and consequently un-schedule cron jobs, use this command. 

$ crontab –r

This command is run by a root user to delete cron jobs for a specific user.

$ crontab -r -u username

Now, let’s continue to commands that require administrative privileges. These commands are run using sudo as shown below. 

To view the crontab of the user carol, use this command. 

$ sudo crontab -l -u carol

This command edits the crontab of the user carol. 

$ sudo crontab -u carol –e

This removes the crontab for the user carol.

$ sudo crontab -r -u carol

Cron Operators.

Cron operators are special characters that enable us to specify multiple values in a cron field. 

They include the following:

  • An asterix (*) specifies all values in a field, e.g. an asterix in the days field means a job will run every day.
  • A comma (,) separates items in a list, e.g. using “Mon, Fri” in the fifth field means the job will run on Mondays and Fridays only.
  • A dash () defines a range of values, e.g. 2000-2005 in the year field means every year between 2000 and 2005.
  • A forward slash (/) divides a value, e.g. */3 in the hours field means every 3 hours.

Scheduling cron Jobs.

A cron expression is a string with five or six fields separated by a space and it represents a time when a cron job will run. This uses a 24-hour format. 

To be ready for execution, a cron command needs to have the cron file and a cron expression. Here are some examples of when and how to schedule cron jobs.

Schedule a Job to Run at a Specific Time.

Let’s take for instance the script mybackup.sh should be executed on 15th January at 11:15 AM. We use:

15 11 15 01 * /home/jobs/mybackup.sh 

Here is the interpretation of the command: 

15 – 15th minute
11 – 11 AM.
15 – 15th day
01 – 1st month i.e. January
* – All days of the week

Schedule a Job to Run Twice a Day.

Here’s how to run script mydailybackup.sh every day at 10 AM. and 5 PM. 

00 10, 17 * * * /home/jobs/bin/mydailybackup.sh 

The command is interpreted as shown below: 

00 – 0th minute, beginning of the hour
10, 17 – 10 AM. and 5 PM.
* – Daily
* – Monthly
* – All days of the week

Schedule a cron Job That Runs Every Day for a Range of Time.

This example checks the status of the database every day from 8 AM to 5 PM. 

00 08-17 * * * /home/jobs/bin/checkdbstatus 

Below is the meaning of the command. 

00 – 0th minute, beginning of the hour
08-17 – 8 AM. to 5 PM.
* – Daily
* – Monthly
* – All days of the week

Schedule a cron Job to Run Every Specified Number of Minutes.

This example checks the disk space every 30 minutes. 

*/30 * * * * /usr/bin/jobs/check-disk-space 
cron command in linux

Predefined cron Strings.

There are special strings written in plain English that can be used instead of the usual cron expression codes, such as those listed below.

@yearly or @annually is equivalent to 0 0 1 1 * and runs once a year on 1st January at midnight.
It can be ran as:

@yearly /home/jobs/bin/yearly-check

@monthly is equivalent to 0 0 1 * *. This cron job runs at midnight on the first day of every month.
It can be ran as:

@monthly /home/jobs/bin/monthly-check

@weekly is represented by 0 0 * * 0. This job runs at midnight on the first day of the week.
It can be ran as:

@weekly /home/jobs/bin/weekly-backup 

@daily or @midnight is equivalent to 0 0 * * * and this job runs once a day at midnight.
It can be ran as:

@daily /home/jobs/bin/weekly-backup

@hourly is equivalent to 0 * * * * and will execute once an hour at the beginning of the hour.
It can be ran as:

@hourly /home/jobs/bin/hourly-check

@reboot  job will execute when the system starts.
It can be ran as:

@reboot /path/bin/execuable01 

This concludes our article on how to use the cron command in Linux. If you have any questions or suggestions, let us know in the comment section below.

Click here to learn more on How to Use LDD Command in Linux

If this guide helped you, please share it. 🙂

Author

Leave a Reply

Your email address will not be published. Required fields are marked *