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,281FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

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.

CopyQ – Advanced clipboard manager for Linux

We have all had that moment when we copied a text, but we first needed another one, and in the process, lost the first one. If you're lucky, you can get it back quickly with a bit of work.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

15 Tar command in Linux uses with examples

Tar is a famous utility that is basically used for collecting multiple files in a single archive. This file is often called a 'tarball'. Today, we are going to tell you about the different things you can do with tarballs using the tar command, with practical examples.

How to make Ubuntu look like Windows XP

If you are in a nostalgic mood and would like to make your Ubuntu look like Windows XP, you just landed in the right place. In this article, we will show you how you can make Ubuntu look like Windows XP by applying the GTK+ theme. Our Ubuntu distribution of choice is Ubuntu 20.04 LTS.