How to Schedule Tasks on Linux Using Crontab?

How to Schedule Tasks on Linux Using Crontab?

6 mins read275 Views Comment
Updated on Aug 3, 2023 15:54 IST

In the fast-paced world of Linux system management, time-saving solutions are always welcome. Crontab, a powerful task scheduling tool, brings a myriad of benefits to the table. From automating routine tasks like backups and system maintenance to optimizing overall productivity, Crontab’s efficiency lies in its ability to execute recurring tasks without constant manual intervention. By leveraging a straightforward time and date component setup, users can effortlessly configure repetitive tasks, giving them more freedom to focus on critical activities. In this article, we explore the advantages of using Crontab on Linux and how it can revolutionize your system management workflow. Embrace the convenience and reliability of Crontab and unlock the full potential of automation in your Linux environment.

2023_03_MicrosoftTeams-image-331.jpg

We do little things on a daily basis. But what if we schedule tasks that we do on a daily basis to enhance our productivity? Whether you are an individual person or a business, scheduling tasks can leverage you with productivity and efficiency in your tasks.

Explore popular Linux Courses

And Linux systems deliver a wide range of flexibility to automate your day to day tasks and save you time & effort. In this article, we will explore how to schedule tasks on Linux using the crontab. But, before that, it is also important to brief about scheduling tasks and crontab on Linux.

Let us head straight into the article.

The need to schedule tasks on Linux

The need for task automation on Linux allows users to automate their day to day tasks without doing it manually. Repetitive tasks can be assigned to computer using multiple channels. This in term increases work few productivity for businesses. It even ensures productivity on an individual level. However, you must identify what tasks you can automate to save your time and efforts. These tasks can be backing up files on fixed recursive intervals or completing data entry to achieve efficiency in work instead of manually doing the same.

You can automate:

  • Daily and weekly tasks that are repetitive in nature
  • Tasks that does not require creative skill set or complex details
  • Monthly tasks that are fixed like backing up your data and so much more

But what benefits do you get when you automate or schedule jobs on Linux systems?

Benefits of scheduling jobs on Linux systems

When you opt to automate your recursive tasks, you get ample benefits that are essential for your individual and organizational growth. Here are a few of the benefits of scheduling jobs on Linux:

  • No chance of errors compared to manually performing the jobs
  • High level of control and efficiency since you can modify your scheduled job anytime and make it complex
  • The faster response since the computer performs your tasks at the fastest pace
  • Assists users with a high level of satisfaction in their task completion
  • Improves productivity since automating tasks saves you time and efforts

Now that we are very much versed in task automation or scheduling jobs, we are ready to head into the core of our article. Let us learn about one of the popular techniques to schedule jobs on Linux, i.e., crontab.

What is crontab in Linux systems?

Crontab, short for Cron Table, in Linux systems, is a command that helps users to create a list of commands that the computer can execute on a specified time and schedule. The computer performs the mentioned command on its own, and users do not have to worry about performing it manually. You can highly automate the desired tasks that you perform daily using the Crontab command.

Linux systems have a default crontab file that users can modify according to their introduced set of commands. In simple words, on crontab, a user tells the Linux machine to “run the following command at this time and date.” However, the location of crontab depends on different operating systems. On the Ubuntu Linux operating system, the location of the Crontab file is placed in /var/spool/cron/crontabs. A user can have multiple crontab files.

Common crontab commands to know

  • crontab -e UserName: Users can edit the crontab file
  • crontab -l UserName: Displays cron jobs
  • crontab -r username: Removes crontab file

Also read: Difference between Linux and Unix

How to schedule tasks on Linux using crontab?

Here are the complete steps to schedule a task on Linux using crontab.

Step 1: Open the terminal using ctrl + alt + t or from the app launcher on your Linux system.

Step 2: Use the command crontab -e to open the crontab files on your Linux system.
Note: If your Linux system does not use sudo, you can use su-c “crontab -e” command.

Step 3: Now, the terminal will ask you to choose your default text editor in case you are opening the crontab for the first time on your Linux system. Hit 1 if the system asks for a text editor choice. Once you are in the editor, you will see the below screen.

Use the arrow key to scroll down to the bottom of the terminal screen.

Step 4: Here, you can add new tasks. Crontab uses the following variables to specify the time of the job.

minute(0-59) hour(0-23) day(1-31) month(1-12) weekday(0-6) command

Syntax:

[Schedule] COMMAND

Note 1: You can run multiple commands. Let us see a live test case with the command.

Note 2: In case you want a command to run every day, you can use astrick in all other values.

Also Read: Top Linux Interview Questions and Answers

Test Case Example

Problem: Support you as a productive person and focus on planning your day every day at 7 am. You have to open your text editor at 7 am on your Linux machine, which takes time. Now you want the system to automatically open your text file daily at 7 am, no matter what you are doing.

Solution: In this test case, you can use crontab to schedule your job on the Linux system. You can open the TO-DO text file on a daily basis at 7 am in the morning. Here is how you will write the command in the terminal.

Here asterisks represent that we want to repeat the tasks on a regular basis. In case you want to perform a task on a daily basis, you will leave the date and month columns with asterisks.

We wrote 7 in the hour section, which is 7 am every day. The command /Desktop/Daily to do tasks will open the Daily to do tasks file saved in the desktop folder every day at 7 am.

Now, you can save the updated file by pressing ctrl + O, which writes out the file and saves it.

Once your file is saved, you can cross check it from the directory /var/spool/cron/crontabs.

This is how you can automate your commands and let them automatically provoke at the specified time.

Also Read: Top Unix Interview Questions and Answers

Ending words- schedule tasks on Linux using crontab

Crontab is a fantastic way to schedule tasks on Linux using crontab. When you automate your recursive tasks, you unlock the benefit of efficiency and utmost productivity. You can automate as many tasks as you want by defining the time variable followed by the command you want to execute. If you want to explore more about Linux commands, you can check out basic Linux commands to further enhance your knowledge of the Linux ecosystem.

Author: Mohit

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio