SnapShooter Learning Center

How to Set Up a Cron Job in Linux

How to Set Up a Cron Job in Linux

Simon Bennett]
Simon Bennett
Last Updated: Sep 28, 2021
Table of Contents

# Introduction

If you are a system admin or developer then you may often need to run specific tasks automatically without any manual intervention. This is the place where the Cron jobs come into the picture. Cron jobs help in automating such tasks — more specifically, those repetitive tasks.

The Cron software is a job scheduler in Linux operating systems. It allows Linux users to run a specific command or script at a given time and date. Generally, it is used by system administrators to automate backup tasks, directory cleaning, notifications, etc.

The Cron jobs runs in background and continuously checks /etc/crontab, /etc/cron.*/ and /var/spool/cron/ directories for any jobs and execute them at pre-defined interval of time.

In this post, we will show you how to set up a Cron job in Linux.


# Requirements

  • A server running Linux operating system.
  • A root password is set up on your server.

# Install Cron Package

Before starting, make the sure Cron package is installed in your system. If not installed you can install it using the following command:

apt-get install cron -y

Or

dnf install crontabs -y

After installing the Cron package, start the Cron service and enable it to start at system reboot:

systemctl start cron
systemctl enable cron

Or

systemctl start crond
systemctl enable crond

# Cron Basic Syntax

The basic syntax of Cron is shown below:

A B C D E USERNAME COMMAND

A brief explanation of each syntax is shown below:

  • A - Minutes range from 0 to 59
  • B - Hours range from 0 to 23
  • C - Days range from 0 to 31
  • D - Months range from 0 to 12
  • E - Days of the week range from 0 to 7
  • USERNAME - User that run the Cron job
  • COMMAND - Command that you want to execute

# How to Set Up a Cron Jobs

In this section, we will show you how to set a cron job with example.

You can create a Cron job by editing /etc/crontab file. Run the following command to edit the crontab file:

crontab -e

This will open a nano or vim editor in your terminal. You can add your Cron jobs here.

To set up a cron that runs every 5 minutes, add the following line:

*/5 * * * * root  /path/of/command

To set up a cron that runs at 6 p.m. every day, add the following line:

0 18 * * * root /path/of/command

To set up a cron that runs at the 15th minute of every 2nd and 9th hour, add the following line:

15 2,9 * * * root /path/of/command

To set up a cron that runs at every hour, add the following line:

0 * * * * root  /path/of/command

To set up a cron that runs the command every Tuesday, Wednesday, and Thursday at 4:00 AM.

0 4 * * 2-4 root /path/of/command

To set up a cron that runs every Monday at 1 a.m., add the following line:

0 1 * * */MON root /path/of/command

To set up a cron that runs on the first day of every month and Sundays at 12 a.m., add the following line:

0 0 1 * 0 root /path/of/command

You can also use the following strings in the crontab file to set up your jobs quickly.

  • @hourly: Run once every hour.
  • @midnight: Run once every day.
  • @daily: same as midnight.
  • @weekly: Run once every week.
  • @monthly: Run once every month.
  • @yearly: Run once every year.
  • @reboot: Run once at every startup

# Manage Cron Jobs

To list all Cron jobs, run the following command:

crontab -l

To list other user's Cron jobs, run the following command:

crontab -u username -l

To delete all Cron jobs, run the following command:

crontab -r

To delete a Cron job for a specific user, run the following command:

crontab -r -u username

To edit the specific user's Cron jobs, run the following command:

crontab -u username -e

# Conclusion

In the above guide, you learned how to set up a Cron job on Linux. Cron is a simple and powerful utility that helps you to reduce the burden of many tasks associated with system administration. You can now automate many tasks with Cron.


Did you find this article helpful?