Skip to Content

How to Setup Crontab in Linux with Examples

This tutorial will walk you through creating Cron jobs via Crontab in Linux.  We'll go over what Cron and Crontab are, how to install Cron, and how to set up scheduled tasks to run in the background.

What is Cron?

Cron is a time-based job scheduler that allows you to run commands or scripts at any given date and time in Linux-based environments using the crontab tool.

A scheduled task, also known as a cron job, can be used in many different scenarios including backing up a database, clearing out temporary file storage locations, and much more.

Cron jobs are great for handling behind-the-scenes tasks that run regularly and are not generally user-executed.

How to Install Cron

First, you'll need to install Cron in your Linux environment.

To get started, you'll need to make sure you have the latest packages installed on your server:

apt-get update

Once the latest Linux packages are updated, you can install Cron:

apt-get install cron

Then verify that the service is up and running:

sudo systemctl status cron

Configuring Your Cron Jobs with Crontab

Open your Cron configuration file, crontab, in edit mode with the following command:

crontab -e

As a rule, each Cron job you create gets separated on a new line. And all parameters are space-delimited. Here's the standard format:

* * * * * /path/to/command [arg1, [arg2]]

There are six required parameters and two optional arguments. The first five parameters in the command represent numbers defining the date and time the Cron job should run. And the remaining values represent the command or file to run, or URL to hit.

Let's break down each one in the order they should be input:

  • Minute: Possible values include 0 - 59. To run a Cron job every time the system clock shows 7 in the minute's position, use this syntax: 7 * * * *
  • Hour: Possible values include 0 - 23. To run a Cron job every time the system clock shows 7 in the hour's position, use this syntax: 0 7 * * *
  • Day: Possible values include 0 - 31. To run a Cron job every time the calendar day hit's the 7th day of the month, use this syntax: 0 0 7 * *
  • Month: Possible values include 0 - 12, where 0 = none, 1 = January, 2 = February, etc. To run a Cron job on the 7th month of the year, July, use this syntax: 0 0 0 7 *
  • Day of the Week: Possible values include 0 or 7 = Sunday, 1 = Monday, 2 = Tuesday, etc. To run a Cron job every Sunday, use the following syntax: 0 0 * * 7

Crontab Examples

Let's run through some Crontab examples and possible scenarios.

Run a script at midnight on a daily basis:

0 0 * * * /path/to/script.sh

Run a script at midnight every Saturday only:

0 0 * * 6 /path/to/script.sh

Run a script every 15 minutes. Here, the * means "all times" and the / tells it to repeat. In this case, every 15 minutes:

*/15 * * * * /path/to/script.sh

The same would be true if you wanted to repeat a script execution every 2 hours:

0 */2 * * * /path/to/script.sh

Run a script Monday through Friday between 9:00am and 6:00pm only:

0 09-18 * * 1-5 /path/to/script.sh

Back up a single MySQL database with a filename including the current date at 5:00am daily. Make sure to replace the values between [ and ] with your own values:

0 5 * * * mysqldump -u [username] --password=[password] [db-name] > /home/ubuntu/mydb-$(date +\%Y\-\%m\-\%d).sql

Hit a specific URL once every hour:

0 */1 * * * wget https://www.google.com
Don't actually hit Google programmatically once every hour.

Restarting the Cron Service

Any time you make updates to the Crontab configuration file, you'll need to restart the crontab service:

sudo systemctl restart cron

If Cront restarted with no errors, then you should not see anything output to the console.

However, if there were errors, it will tell you exactly where the error occurred in the configuration update so you can easily track it and update as needed.

Once restarted, your Crontab configuration updates will go into affect immediately.

Other Useful crontab Commands

  • crontab -l: Lists all of your Cron jobs.
  • crontab -r: Deletes all of your Cron jobs.
Be careful when using the crontab -e command as the e and r keys are right next to each other on a standard keyboard. Using the wrong flag could inadvertently delete all your Cron jobs in your configuration file.

Conclusion

You should now have a solid understanding of how to create and manage Cron jobs in Linux via Crontab.

Cron jobs are great for handling behind-the-scenes tasks that run regularly and are not generally user-executed.

Use the examples shown in this article to help you get started. Over time, you'll be a Linux task scheduling guru.

Last Updated: September 22, 2022
Created: November 30, 2020

Comments

There are no comments yet. Start the conversation!

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.