The “crontab” Command in Linux [10 Practical Examples]

The crontab command in Linux is used for scheduling tasks at regular intervals. You can add your desired commands/scripts to crontab files by creating an editing session. The listed jobs in the crontab files are run by the cron daemon at predefined times.

A. Description

The crontab command in Linux is the abbreviation for “Cron Table”. It is a program that drives the cron daemon in Linux-like OS. Each user can have their individual crontab files. You can edit these files to schedule tasks in your system. crontab maintains the crontab files for each user. In order to schedule processes using the crontab command in Linux you must be listed in the /etc/cron.allow file. If this file does not exist and another file /etc/cron.deny exists, then must not be listed in this file.

B. Syntax

The syntax of the crontab command in Linux has multiple combinations. You can specify a user’s name while accessing their crontab files. Below I am giving the detailed syntax for the crontab command in Linux.

crontab [ -u user ] file
crontab [ -u user ] [ Option ]

C. Options

The crontab command in Linux comes with several options. Here, I am listing the options with explanations for your use. You can also look at the man page to explore further.

man crontab

Useful Options

  • -u ( Specify the user name whose crontab file is accessed )
  • -e ( Edit current crontab using specified editor )
  • -l ( Display/List current crontab on the standard output )
  • -r ( Remove current crontab )
  • -i ( Ask before removing crontab using -r )

NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

The “crontab” Entries in Linux

Using the crontab command in Linux you can insert commands/scripts in crontab files. These commands will execute at your given time. To achieve this, you will need to follow a certain syntax and use special operators or keywords while creating entries in the file.

A.   The syntax for “crontab” Jobs

The syntax given below is a basic format for inserting a job into a crontab file. The first five values indicate when and how often a job/command will execute. These values are separated by a space presenting each individual value.

a b c d e commandSyntax for cron jobs using crontab command in Linux.
Disclaimer: You cannot leave any field blank and their order must be followed exactly.

B.   Operators for “crontab” Jobs

The crontab command in Linux offers the following operators to set a specific time for cron jobs. You can use these operators separately or along with determined values. Following are the operators you can use while assigning cron jobs.

  • Asterisk [ 🞸 ], ( Denotes all possible values i.e. 🞸 in the minute field indicates 0,1, 2…59 minutes )
  • Comma [ , ] ( Specifies a list i.e. 1, 3, 5, … formate denotes exactly listed values )
  • Hyphen [ – ] ( Specifies a range i.e. range 0 – 3 formate denotes values 0, 1, 2 and 3)
  • Separator [ / ] ( Skips number of values given after / i.e. */2 in the hour filed will run a task past every 2nd hour )

C.   Special Keywords for “crontab” Jobs

You can use a number of special keywords while scheduling jobs using the crontab command in Linux. Most of these keywords have equivalent fields in the crontab job syntax. Some of the keywords and their description are given below.

Keywords Equivalent Fields Description/ Meaning
@hourly 0 * * * * Executes scheduled task at 0 minutes of every hour
@daily/@midnight 0 0 * * * Executes scheduled tasks at 00:00 of every day
@weekly 0 0 * * 0 Executes scheduled tasks at 00:00 of every Sunday
@monthly 0 0 1 * * Executes scheduled task at 00:00 on 1st day of every month
@yearly 0 0 1 1 * Executes scheduled task every year at 00:00 on 1st day of January
@reboot N/A Executes scheduled tasks at the system startup

Practical Examples of the “crontab” Command in Linux

With the crontab command in Linux, you will be able to set/ remove commands to execute at a certain time. You will also be able to explore other users’ crontab files. In this section, I will illustrate some of the most common uses of the crontab command in Linux.

Example 1: Editing Crontab Files Using the “crontab” Command in Linux

You will be able to edit the crontab files of your system using the crontab command in Linux with the option -e. In this example, I will open my crontab file in editing mode using the nano text editor. To do so you can follow the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

crontab -e

➌ Now, press the ENTER button.

Output >

You can see in the following image, the crontab configuration file has been opened in the nano text editor using the mentioned command. The command has given me permission to write on the crontab file.Editing crontab files using crontab command in Linux

crontab file opened in the text editor nano.

Example 2: Editing Crontab Files of Specified User Using the “crontab” Command in Linux

You can edit the crontab files of other users using the crontab command in Linux with the combined options -u and -e. But to do so you will need to run commands as the superuser using the sudo command. In this example, I will open the crontab file of a user named “pseudo”. I will choose the nano as my text editor. To do so you can follow the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo crontab -u pseudo -e

➌ Press the ENTER button.

➍ Enter your password.

➎ Now, press 1 to choose nano as the editor

➏ Now, Press the ENTER button.

Output >

In the first image, you can see that I ran the command as the supersuer and selected the nano for the editor.Opening other users' crontab using sudo command and choosing nano as the editor. In this image, you can see that I have permission to write on the crontab file of the user “pseudo”.Opened crontab file of the user "pseudo" in editor.


Similar Readings


Example 3: Viewing Crontab Entries Using the “crontab” Command in Linux

Using the crontab command in Linux with the option -l you can display the contents of your crontab file on the Terminal. In this example, I will demonstrate opening my crontab file on Terminal. To do so you can follow the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

crontab -l

➌ Now, press the ENTER button.

Output >

You can see in the following image, the contents of my crontab configuration file are being displayed on the Terminal.Displaying contents of the crontab file.

Example 4: Viewing Crontab Entries of Specified User Using the “crontab” Command in Linux

Using the crontab command in Linux with the options -u and -l you can display the crontab file contents of a specific user on the Terminal. But to do so you will need to run commands as the superuser typing the sudo command. In this example, I will display the crontab file contents of user “pseudo” on Terminal. To do so you can follow the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo crontab -u pseudo -l

➌ Press the ENTER button.

➍ Enter your password.

➎ Now, Press the ENTER button.

Output >

In the image below, you can see that the crontab configuration file contents of user “pseudo” are being displayed on the Terminal. Displaying contents of the crontab file of another user.

Example 5: Removing Crontab Tasks Using the “crontab” Command in Linux

You can remove your crontab from the command line using the crontab command in Linux. You will need to type the option -r along with the crontab command. In this example, I will remove my crontab file. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

crontab -r

➌ Press the ENTER button.

Output >

In this image, you can see that my crontab configuration file has been deleted. It showed the following message when I tried to display my crontab file using the -l option.Removing user's own crontab file.

Example 6: Removing Crontab Tasks of Specified User Using the “crontab” Command in Linux

You can remove your crontab file of other users from the command line using the crontab command in Linux. For this, you will need to type the option -r along with the sudo command. In this example, I will remove the crontab file of user “pseudo”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo crontab -u pseudo -r

➌ Press the ENTER button.

➍ Enter your password.

➎ Now, Press the ENTER button.

Output >

In this image, you can see that the crontab configuration file of “pseudo” has been deleted. It showed the following message when I tried to display the contents of that crontab file using the -l option.Removing other user's crontab file using sudo command.

Checking deleted crontab file of other user using -l option.

Example 7: Display Confirmation Message Before Removing Crontab Tasks Using the “crontab” Command in Linux

The crontab command in Linux provides an option to display confirmation messages while removing crontab files. You will need to combine the options -i and -r along with the crontab command. In this example, before deleting my crontab file I will show a confirmation message. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

crontab -i -r

➌ Press the ENTER button.

➍ Now, press the y key.

Output >

In this image, you can see that the system is asking for confirmation before deleting my crontab file. I had to enter y to remove the file. Displaying confirmation message before deleting crontab file using crontab command in Linux. Here, I tried to display the crontab after the previous command. The system ensured that the file no longer exists.Checking deleted file with -l option using crontab command in Linux.


Similar Readings


Example 8: Scheduling a Job for Every Minute Using the “crontab” Command in Linux

You can schedule a task to execute at regular intervals using the crontab command in Linux. For this, you will need to edit the crontab file using the -e option. In this example, I will schedule a command to run every minute on my machine. You may do so by following the given process.

Steps to Follow >

➊ At first open the Ubuntu Terminal

➋ Type the following command in the command prompt:

crontab -e

➌ Press the ENTER button.

➍ Type the following command at the bottom of the crontab configuration file opened in your text editor:

* *   *   *   *   echo "1 Minute passed! Date & Time: $(date)" >> time.log

➎ Press the CTRL+S keys to save changes.

➏ Now, Press the CTRL+X exit the editor.

Output >

In the image below, you can see that using the nano editor I entered my desired command at the end of the crontab file. I applied the Asterisk ( 🞸) operator to indicate every minute.Editing crontab file using crontab command in Linux. To view the output of the task further, I redirected it to the “time.log” file using the append (>>) operator.Adding a cron job to run every minute using crontab command in Linux. You can use the tail command to view outputs printed on the “time.log” file.Viewing output of cron job using tail command in Linux.

Example 9: Scheduling a Task for a Certain Time Period Using the “crontab” Command in Linux

You can schedule a task to execute at a certain period using the crontab command in Linux. For this, you will need to edit the crontab file using the -e option. In this example, I will schedule a command to run on the first 3 minutes excluding the 0th minute of every hour. You may do so by following the given process.

Steps to Follow >

➊ At first open the Ubuntu Terminal

➋ Type the following command in the command prompt:

crontab -e

➌ Press the ENTER button.

➍ Type the following command at the bottom of the crontab configuration file opened in your text editor:

0-3 *   *   *   *   echo "1st 3 minutes of hour: $(date)" >> ThreeMin.log

➎ Press the CTRL+S keys to save changes.

➏ Now, Press the CTRL+X exit the editor.

Output >

In the image below, you can see that using the nano editor I entered my desired command at the end of the crontab file. I applied the Hyphen ( – ) operator to indicate the first 3 minutes and the Asterisk ( 🞸) operator to indicate every hour.Editing crontab file using crontab command in Linux. To view the output of the task further, I redirected it to the “ThreeMin.log” file using the append (>>) operator.Adding a cron job to run at the first 3 minutes of every hour excluding 0th minute using crontab command in Linux. You can use the tail command to view outputs printed on the “ThreeMin.log” file.Viewing output of cron job using tail command in Linux.

Example 10: Running a Task After Each System Startup/Reboot Using the “crontab” Command in Linux

You can schedule a task to execute at each system startup using the crontab command in Linux. You will need to edit the crontab file using the -e option and use the “@reboot” string before your desired command. In this example, I will schedule a command to run on each system reboot. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal

➋ Type the following command in the command prompt:

crontab -e

➌ Press the ENTER button.

➍ Type the following command at the bottom of the crontab configuration file opened in your text editor:

@reboot  echo "1 line at each reboot. Date: $(date)" >> eachReboot.txt

➎ Press the CTRL+S keys to save changes.

➏ Now, Press the CTRL+X exit the editor.

Output >

In the image below, you can see that using the nano editor I entered the above-mentioned command at the end of the crontab file. To view the output of the task, I redirected it to the “eachReboot.txt” file using the append (>>) operator.Editing crontab file using crontab command in Linux.

Adding a cron job to run at each system reboot using crontab command in Linux.You can use the cat command to view outputs printed on the “eachReboot.txt” file. It will show the exact times when the system was rebooted and updated the file.Viewing output of the cron job using cat command in Linux.

Conclusion

In this article, I have tried to illustrate the basics of the crontab command in Linux. Scheduling tasks for users is considered to be crontab’s main purpose. You will find Job scheduling as well as other examples of crontab in this writing. I hope these practical examples will help you through your journey with the command line and make you a power user of Linux.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment