Crontab in Linux Explained With Examples

Do you want to know the full potential of Crontab and everything you can do with it? Here are practical examples of how to use Crontab to schedule tasks in Linux.

Crontab is the Linux equivalent of the Window’s Task Scheduler. It can help you set up a task to run automatically at a regular interval. Some of you might already be thinking up various use cases for this functionality. It is most useful to sysadmins who can use it to automate routine maintenance jobs and other tasks.

To help you understand the full potential of Crontab and everything you can do with it, we will go over a handful of practical examples of how to use Crontab to schedule tasks on your Linux system. But first, let’s get a basic understanding of the crontab syntax.

Crontab Syntax – How to Write Crontab Entries

When writing a crontab entry, you have to work with six fields. The first five fields 1-5 are used to specify the date and time when you want the task to run, whereas the sixth field is to define the concerned task.

Here is a basic look of how a Crontab entry should look like:

[minute] [hour] [DayOfMonth] [MonthOfYear] [DayOfWeek] [the script you want to execute

The acceptable values for each of these fields are as follows:

  • Minute: Value can be between 0-59.
  • Hour: Value can be between 0-23.
  • DayOfMonth: Value can be between 1-31. For months having less than 31 days (say February with 28 days), the extra days will get ignored.
  • MonthOfYear: Value can be between 1-12. Here 1 represents January and 12 represents December. The value can also be the first three letters of the month – for January, you can write Jan, and for June, you can write Jun.
  • DayOfWeek: Value can be between 0-7. Here both 0 and 7 represents Sunday. Also, the value can be the first three letters of the day – for Monday, you can write, Mon, and for Friday, you can write Fri.

Now that you have a basic understanding of how to define the temporal values, let’s quickly go over setting multiple values and ranges. For this, you can use the following special characters:

  • Asterisk (*): this means that the value can match anything.
  • Comma (,): use this to define multiple values like 2,4,6 or Tue, Thu, Sat.
  • Hyphen (-): this can be used to define a range like 2-6 or Tue-Sat. Here all the numbers between 2 to 6 and all the days from Tuesday to Saturday will be taken.

You can also define multiple ranges by using both comma(,) and hyphen (-) together. For example, if you want to define all the months from January to April and October to December, you can write jan-apr,oct-dec.

How to create or add a New Crontab entry?

So, now that you have a basic idea of how to write Crontab entries let’s see how you can add a task to it. Well, first you need to enter the following command in the terminal:

$ crontab -e

This is going to open a Crontab file in the editor where you can add or edit a job.

Crontab Usage
Crontab Usage

By default, it will create the crontab entries for the current user. However, if you want to create a crontab entry for a different user, then you need to use this command instead:

$ crontab -u username -e

The username is just a placeholder where you need to enter the actual username for which you wish to create a new crontab task.

How to view all the Crontab Entries?

Over time, you might have created a bunch of crontab entries on your system. As such, it becomes necessary to review these entries to see which ones you still needed and which ones you can remove.

To list all the crontab entries for the current user, you can use the following command:

$ crontab -l

However, to view the crontab entries of a different user, you will need to use this command instead:

$ crontab -u username -l

So that pretty much covers all the basic syntax and commands you need to use for Crontab. Let’s talk about some practical examples of how you can use Crontab in Linux.

15 Practical Examples of Using Crontab in Linux

Here we will be going over some practical examples of crontab entries that will be useful in real-world circumstances. We will show you the crontab command that you need to write and what it does so you develop a deeper understanding.

1. Schedule a Cron to run daily at a particular time

The following command will run the task /scripts/script.sh, every day at 1 a.m.

0 1 * * * /scripts/script.sh

The asterisk in the third field denotes that it will run every day of the month, the fourth field denotes that it will run every month of the year and the fifth field denotes that it will run every day of the week.

2. Schedule a Cron to run daily

The following command will run the task /scripts/script.sh, daily at midnight.

0 0 * * * /scripts/script.sh

or you can use the command:

@daily /scripts/script.sh

Here @daily timestamp substitutes for “0 0 * * *”.

3. Schedule a Cron to run twice a day

The following command will run the task /scripts/script.sh, every day at 1 a.m and 1 p.m.

0 1,13 * * * /scripts/script.sh

Notice that the second field contains two entries separated by a comma to define the two separate time points.

4. Schedule a Cron to run every minute

The following command will run the task /scripts/script.sh, every single minute, repeatedly.

* * * * * /scripts/script.sh

As you can see, all of the five fields that are used to specify the day and time are marked with an asterisk (*).

5. Run every x minutes

The following command will run the task /scripts/script.sh, every 20 minutes.

* /20 * * * /scripts/script.sh

Here we have used /20 in the hour-field, which means the task will run every 20 minutes. If you had used /10 here, it would mean the task would run every 10 minutes.

6. Run every x hours

The following command will run the task /scripts/script.sh, every 6 hours at time 0 minutes.

0 * /6 * * /scripts/script.sh

We have used /6 in the day-field, which is telling it to run the command every 6 hours. Since the minute-field is 0, it will run exactly when the minute hand is at 12(or 0).

7. Schedule a Cron to run every 10 seconds

To execute a command every 10 seconds is not possible using the time parameters along. However, this can be managed by configuring the same Cron twice while including a sleep command.

* * * * * /scripts/script.sh
* * * * * sleep 30; /scripts/script.sh

8. Run hourly

The following command will run the task /scripts/script.sh, every hour.

0 * * * * /scripts/script.sh

Like before, you can use the alternative command:

@hourly /scripts/script.sh

9. Automate a task monthly

The following command will run the task /scripts/script.sh, every month.

0 0 1 * * /scripts/script.sh

or you can use the command:

@monthly /scripts/script.sh

10. Execute on a selected month

The following command will run the task /scripts/script.sh, only in May. However, it will execute every single minute or every single day of that month.

* * * may * /scripts/script.sh

If you want the task to run on every May, but only on the 1st at 12 a.m, then use this command instead:

0 0 1 may * /scripts/script.sh

11. Schedule a Cron to run on the first Monday of every month

This is a tricky one. By using the time-parameters alone, you can’t create a Cron that executes only on the first Monday of every month. However, you can add a condition in the command field that will help you do it.

0 0 * * sun [$(date +%d) -le 01] && /scripts/script.sh

12. Have it run weekly

The following command will run the task /scripts/script.sh, every week on Mondays at midnight.

0 0 * * mon /scripts/script.sh

or you can use the alternative command as we used before:

@weekly /scripts/script.sh

13. Yearly

The following command will run the task /scripts/script.sh yearly on the first of January at midnight.

0 0 1 1 * /scripts/script.sh

There is also an alternative command for this as well:

@yearly /scripts/script.sh

14. Run multiple tasks once a day at a particular time

The following command will run these two tasks /scripts/script1.sh and /scripts/script2.sh, every day at midnight.

0 0 * * * /scripts/script1.sh; /scripts/script2.sh

Using the semicolon (;), we can define two tasks instead of one. You can use the procedure to define three, four, … up to n number of functions.

15. Schedule a Cron to run after a system reboot

Sometimes it can be necessary to run a task or a set of tasks every time after a reboot. This is where “@reboot” becomes useful. The following command will run the task /scripts/script.sh on system startup.

@reboot /scripts/script.sh

Wrapping Up

So this was our in-depth look at Crontab in Linux and how it can help you automate and schedule repetitive tasks. We hope you found the read to be useful and that it helped you in better understanding Crontab and all that you can do with it. What unique usage did you think of, or applied it to your system with Crontab?

Nitish.S
Nitish is a Technical Writer with five years of experience. He enjoys covering new tech and has a special love for Linux. He also has a keen interest in Blockchain and WordPress.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,419FansLike
377FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Basics of Working with the SQLite Database in Python

A database is one of the most useful and popular files for storing data; they can be used to store any kind of data, including text, numbers, images, binary data, files, etc. SQLite is a relational database management system based on the SQL language. It is a C library, and it provides an API to work with other programming languages, including Python. It does not require a separate server process to be run as needed in large database engines like MySQL and Postgresql.

5 Ways to Check the Linux Version

When most people talk of Linux, they are always referring to a Linux distribution. However, this is not the case. Linux itself is a kernel which acts as a bridge between user applications and the hardware. When we talk of a Linux distribution, we refer to an operating system developed from the Linux kernel. A distribution comes with a package manager, pre-installed applications, a Desktop Environment, and several more features.

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

How to Boot your Windows or Linux PC from a USB Drive

Sometime back, the process of installing an operating system required users to pop a bootable media disk into their DVD or CD drive and use it to boot the PC. But times have changed. Nowadays, the most common way of installing an OS is booting from a USB drive. The use of USB drives is further propelled by the current production of slim and lightweight laptops with no support for DVD/CD drives.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

6 cool and fun Linux commands you don’t want to miss

The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

The 10 Best Linux Server Distros For Home And Businesses

By the year 2020, it is estimated that there are close to 600 Linux distributions in the market. It includes both servers and Desktop versions; therefore, if you are looking for lightweight Linux distribution for your old PC or a reliable desktop version for employees in your organization, you may be overwhelmed with the number of choices for finding one for your use.

Top 10 reasons to use KDE as your Desktop Environment

In this article, we will take a look at yet another popular desktop environment, KDE. It's one of the most amazing Linux Desktop environments available in the market.