Skip to main content

How to schedule tasks using the Linux 'at' command

The 'at' command is most useful for scheduling one-time jobs in Linux.
Man wearing a wristwatch

Photo by Marius Mann from Pexels

Time is precious, making time management an appreciated virtue in every aspect of life, whether you're talking about financials, technology, or any other daily activity.

To manage time, a skilled sysadmin must know when and how to control tasks so that they can be programmatically executed at certain times, whether recurring or a set number of times. You can apply this concept in numerous scenarios, from scheduled backup tasks to collecting system logs periodically.

[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]

You can accomplish task scheduling in numerous ways. In this article, I focus on a straightforward tool available on Linux operating systems to help achieve this goal: the at command. My colleague Seth previously wrote a great article about at, so I recommend you check it out, as well as my article about the cron command, another Linux scheduling tool.

This article aims to be as brief, straightforward, and practical as possible, meaning I won't be able to explore all available options for the at utility. Let's get started!

Install the 'at' utility

Depending on your Linux distribution, the at utility may or may not be installed by default. You can install it using your distribution's package manager if it's not installed. For Red Hat Enterprise Linux (RHEL)-based distributions:

$ sudo dnf install at

The at package installs other binaries that are used together with the main command:

$ sudo dnf repoquery -l at | grep bin

The package provides the atd daemon, which you'll interact with through the use of the at and atq commands:

$ sudo systemctl status atd

● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2022-11-09 17:47:48 -03; 14min ago
 Main PID: 1378 (atd)
    Tasks: 1 (limit: 23643)
   Memory: 428.0K
   CGroup: /system.slice/atd.service
           └─1378 /usr/sbin/atd -f

nov 09 17:47:48 demo.example.local systemd[1]: Started Job spooling tools.

When to use the 'at' utility

The at and batch (at -b) commands read from standard input or a specified file. The at tool allows you to specify that a command will run at a particular time. The batch command will execute commands when the system load levels drop to a specific point. Both commands use the user's shell.

Install the at package if you need a utility for time-oriented job control. If you have a recurring job that repeats at the same time every day, week, and so forth, use crontab instead.

For more detailed information on all the possible parameters and examples of how to use at, read the man page:

$ man at

[Cheat sheet: Old Linux commands and their modern replacements ]

Schedule tasks with the 'at' command

I'll show you how at works. First, I'll establish the time frame:

$ date
fri nov 11 15:21:58 -03 2022

Now, there are some different ways to interact with the at utility. One of them is using its interactive command prompt. Do this by typing at {runtime} and pressing Enter:

$ date
fri nov 11 15:26:21 -03 2022

$ at 15:27
warning: commands will be executed using /bin/sh
at> echo "It works!" > /tmp/test.txt
at> <EOT>
job 2 at Fri Nov 11 15:27:00 2022

I defined a command to run at 15:27 (3:27 pm GMT-3) after defining my runtime and entering the at command prompt. In this case, the command creates a text file. To exit the command prompt, type Ctrl+D. The at utility then queues the command to execute later, according to the runtime definition. For an overview of the pending jobs for the current user, use the atq or at -l commands:

$ atq
2	Fri Nov 11 15:27:00 2022 a localuser

From the previous output, you can see the following:

  • 2 is the unique job number.
  • Fri Nov 11 15:27:00 2022 is the execution date and time for the scheduled job.
  • a indicates that the job is scheduled with the default queue a.
  • localuser is the job owner (and the user the job runs as).

[ Want to test your sysadmin skills? Take a skills assessment today. ]

After waiting for 39 seconds, here's what happens when the job gets out of the queue and executes:

$ date
fri nov 11 15:27:01 -03 2022

$ atq

$ cat /tmp/test.txt
It works!

Of course, this is not an ideal way of running scheduled jobs; another way is by pipelining the desired command as an input for the at utility. Learn more about that in my article about manipulating files with shell redirection and pipelines. Here is a quick example:

$ rm /tmp/test.txt

$ echo "It works while pipelining!" > /tmp/test.txt | at 15:41
warning: commands will be executed using /bin/sh
job 3 at Fri Nov 11 15:41:00 2022

$ atq
3	Fri Nov 11 15:41:00 2022 a localuser

$ date
fri nov 11 15:40:40 -03 2022

$ atq

$ date
fri nov 11 15:41:01 -03 2022

$ cat /tmp/test.txt
It works while pipelining!

Use flags with the 'at' command

The most used form of the at utility is specifying an existing script with the -f parameter (or using shell redirection like at {runtime} {} so that at can read the inputs from a file instead of a standard input. Check it out:

$ rm /tmp/test.txt

$ cat << EOF >
> echo "It works while scripting!" > /tmp/test.txt

$ ls

$ sudo chmod +x

$ date
fri nov 11 15:50:58 -03 2022

$ at 15:52 -f ./
warning: commands will be executed using /bin/sh
job 4 at Fri Nov 11 15:52:00 2022

$ atq
4	Fri Nov 11 15:52:00 2022 a localuser

$ date
fri nov 11 15:52:00 -03 2022

$ atq

$ cat /tmp/test.txt
It works while scripting!

You can specify the runtime in different ways, such as:

  • Time:
    • HH:MM
    • HHMM
    • 12-hour format (am or pm)
    • midnight
    • noon
    • teatime
  • Date:
    • MMDD[CC]YY
    • MM/DD/[CC]YY
    • DD.MM.[CC]YY
    • [CC]YY-MM-DD
    • today
    • tomorrow
    • weekday
  • Increment
    • now + count time-units, where the time-units can be minutes, hours, days, or weeks

The -b parameter (or the batch command) allows you to automatically schedule a job to run when system utilization levels are low (load average below 0.8), like this:

$ top -bc | head -1
top - 16:10:04 up 58 min,  1 user,  load average: 0,00, 0,00, 0,00

$ rm /tmp/test.txt

$ ./ | batch
warning: commands will be executed using /bin/sh
job 5 at Fri Nov 11 16:10:00 2022

$ atq

$ top -bc | head -1
top - 16:11:00 up 59 min,  1 user,  load average: 0,00, 0,00, 0,00

$ cat /tmp/test.txt

It works while scripting!

Finally, to remove or cancel a pending job from the queue, you can run at -r or atrm and specify the job number:

$ rm /tmp/test.txt

$ date
fri nov 11 16:16:23 -03 2022

$ at 16:17 -f
warning: commands will be executed using /bin/sh
job 6 at Fri Nov 11 16:17:00 2022

$ atq
6	Fri Nov 11 16:17:00 2022 a localuser

$ atrm 6

$ atq

$ date
fri nov 11 16:16:59 -03 2022

$ cat /tmp/test.txt
cat: /tmp/test.txt: No such file or directory

Put your creativity to the test by imagining situations where you can take advantage of this utility. Essentially, it will be most useful for one-time job scheduling situations.

Wrap up

Knowing how to schedule tasks and jobs on your systems is very important. Some system tasks are already scheduled by default, and you need to understand how they work. In addition, you often need to automate and schedule deferred and recurring jobs to run whenever needed to accomplish programmable goals. The at command will help you with that, and it's a fundamental utility to have in your toolkit.

I hope this and my article on the cron utility aid you in understanding this topic and add to your general sysadmin knowledge.

Check out these related articles on Enable Sysadmin

Author’s photo

Alexon Oliveira

Alexon has been working as a Senior Technical Account Manager at Red Hat since 2018, working in the Customer Success organization focusing on Infrastructure and Management, Integration and Automation, Cloud Computing, and Storage Solutions. More about me

Red Hat Summit 2022: On Demand

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

Related Content


Privacy Statement