How to Use the at Command to Schedule Tasks in Linux?

At Command

The at command in Linux lets you schedule tasks for execution at a later time in the future. These tasks can be execution of commands or running of shell scripts. However, the at command only performs the task once. It can’t be used for performing recurring tasks.

In this tutorial, we will learn how to use the at command. Along with that we will also learn about two other commands. These commands are :

  • atq: Lists all the scheduled tasks that are pending.
  • atrm: Removes scheduled tasks

Let’s get started.

Note : The words job and task are used interchangeably in this tutorial.

How to use the at command to schedule tasks?

Using the at command is pretty straighfrward. You just need to mention the date and time of execution along with the command. However, there are several ways of representing time under the at command. In this section, we will look at the different ways of representing time.

1. Expressing time in HH:MM

You can expressing time in the HH:MM format. The system will execute the tasks whenever the clock matches with the specified time next. Let’s see an example.

at 9:00 AM

When you press enter, you will get the following output :

At 1

This is where you enter the list of commands you want to execute.

at> ls
at> echo "This command was scheduled for 9:00 AM"
at> <EOT>

To end the list, press Ctrl + D.

You will get the following output :

job 1 at Tue Nov 17 09:00:00 2020

Here 1 is the job ID followed by the time of execution.

2. Scheduling tasks for midnight, noon and tea-time.

You can also schedule tasks for midnight, noon and teatime (4 PM) by directly using these words along with at command.

Midnight

To schedule tasks for midnight use

at midnight 
Midnight
Midnight

Noon

To schedule tasks for noon use

at noon 
Noon
Noon

Teatime

To schedule tasks for teatime(4PM) use

at teatime 
Teatime
Teatime

3. Expressing date in MMDDYY

Along with time in HH:MM format, you can specify the date in MMDDYY format.

Let’s take a look at an example.

at 1:30 093021

This will schedule a task for 1:30 AM 30th September 2021.

at> ls
at> echo  "This command was scheduled for 30th September 2021"
at> <EOT>

Output :

job 4 at Thu Sep 30 01:30:00 2021

Alternatively you can also use the MM/DD/YY or DD.MM.YY or YYYY-MM-DD format to express the date.

4. Scheduling tasks using now as the reference point

You can also schedule tasks to take place after a particular amount of time starting from now. You can schedule tasks to take place minutes, hours, days, weeks, months, and years from now. Let’s see how to do that in this section.

Schedule tasks for 1 minute from now :

To schedule tasks for 1 minute from now use the following line of code.

at now + 1 minute

Schedule tasks for 1 hour from now :

To schedule tasks for 1 hour from now use the following line of code.

at now + 1 hour

Schedule tasks for 1 week from now :

To schedule tasks for 1 week from now use the following line of code.

at now + 1 week

Schedule tasks for 1 year from now :

To schedule tasks for 1 year from now use the following line of code.

at now + 1 year

List scheduled tasks using the atq command

You can use the atq command to get the list of commands that are pending for execution.

atq
Atq
Atq

The output shows each job with its job ID, date and time of execution, and the user who scheduled the job.

Remove tasks using the atrm command

You can remove the scheduled tasks using atrm. You need to mention the job ID along with the atrm command.

atrm 2

Output :

Atrm
Atrm

We can see the list of tasks before and after executing the atrm command on job with job ID as 2.

Conclusion

This tutorial was about the at command in Linux. We learned how to schedule tasks using the at command. We also discussed atq and atrm commands. You can explore the command further using the man command.