How to Schedule a Task in Linux?

In Linux, sometimes tasks are performed repeatedly because running them manually is time-consuming and unskilled. For that purpose, Linux OS has a built-in task scheduler, crontab, and at command. It is just like an Asynchronous approach to programming which means a certain task or command will be executed within a given specific time.

This article will demonstrate methods of scheduling a task in Linux. The content for this post is as follows:

Let’s start with the first method.

Method 1: Schedule a Task Using cron Job

The first method for scheduling the task is by using the cron job. It is the job run by the cron daemon at the scheduled time given by the user. To check the scorn service in Linux, run the following command in the terminal:

$ sudo systemctl status cron.service

The cron service status is active and running.

To schedule the cron job in Linux, “crontab” with the “-e” flag will be used. Let’s check it in the terminal:

$ crontab -e

Users can use this file to add, edit, update or delete a job. If we read the above lines, which describe the functionalities that can be done through this file.

The syntax for the cron job is given below:

$ [Minutes] [Hours] [Day_of_Month] [Month] [Day_of_Week] [Command]

The values for the minutes are 0 to 59, hours 0 to 23, days of the month 1 to 31, months 1 to 12, and days of the week 0 to 6.

*” can be used for all possible units such as every minute, every hour, etc.

Example: Printing Date and Time After Every Minute

Let’s implement an example in which we have a “script.sh” file that prints the current date and time and redirects its output to the “date.txt” file. We can schedule the time in which this file executes automatically and will print the current date and time after every minute:

To do so, add the following line in the crontab:

* * * * * /home/itslinuxfoss/script.sh

Save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+x”.

Let’s check the output of the date.txt file after a few minutes:

$ cat date.txt

The current date and time are printed in the file after every minute.

You can assign tasks in minutes, hourly, daily, weekly, or monthly.

  • For Every Minute: * * * * *
  • For Hourly: 0 * * * * ( once an hour)
  • For Daily: 0 0 * * * (once a day)
  • For Weekly:0 0 * * 0 (once a week)
  • For Weekly:0 0 1 * * (once a month)

Lets’ move toward the method 2.

Method 2: Schedule a Task Using “at” Command

Another support for scheduling a task in Linux is using the command. The “at” command is a utility to schedule the command or task at a specific time. In most Linux distributions, it is pre-installed. Still, it can be installed using the following command in case of no availability:

$ sudo apt install -y at

For CentOS/RHEL

$ yum install at

For Arch

$ sudo Pacman -S at

For Fedora

Let’s implement a task using an example,

$ sudo dnf install at

Example: Scheduling echo Command After Specific Time

In the below example, we are scheduling a task of printing the current date and time after 5 minutes using at command.

$ at now + 5 Minutes

Save the job by pressing “Ctrl+D”, and the command will be executed automatically after 5 minutes.

Now, let’s check the output of the “output.txt” file:

$ cat output.txt

The current date and time are printed on the given time.

Bonus Tip: How To Delete a Job at Command?

To delete the job of the “at” command, use the “-r” flag with at command or “at -r” with the job number. We have one job in the queue that can be tested using the given command:

$ at -l

Let’s remove it by running the given command:

$ at -r 8

The successful execution of the command shows that job 8 is removed.

That’s how you can schedule a task in Linux.

Conclusion

To schedule a job in Linux, there are two methods: using a cron job or the “at” utility. Both are built-in support of Linux. This post has illustrated the two most efficient methods of scheduling a task in Linux. Apart from that, the removal method of the job using at command has also been covered in this write-up.