How to Use the Linux at Command

February 2, 2022

Introduction

The at command is a Linux command-line utility used to schedule a job for later execution. The utility reads commands from standard input and groups them into an at job, which executes only once.

The alternative for at is a cron job. However, while at jobs execute only once, cron jobs are recurring events.

In this tutorial, you will learn to use the at command and see useful examples for command scheduling.

How to use the Linux at command, including examples.

Prerequisites

  • A system running Linux.
  • A user account with sudo privileges.

Install the at Command

Depending on which Linux distribution you are using, the at command may not be pre-installed.

Check if at is installed by entering the command name in the terminal:

at command not found error in Linux.

If the utility isn't pre-installed, the output message states Command 'at' not found.

Install at on Ubuntu and Debian

Follow the steps below to install at on Ubuntu or Debian:

1. Update the package repository:

sudo apt update

2. Install the at command by running:

sudo apt install at

Install at on CentOS and Fedora

Follow the steps below to install at on CentOS or Fedora:

1. Update the package repository:

sudo yum -y update

2. Run the following command to install at:

sudo yum install at

Enable Scheduling Daemon

The scheduling daemon runs in the background and executes the scheduled jobs on time.

Run the following command after installing the at package to enable the atd scheduling daemon and set it to start on system boot.

sudo systemctl enable --now atd
Enabling the atd scheduling daemon in Linux.

Linux at Command Syntax and Options

The syntax for the at command is:

at [option] runtime

The options allow you to view or delete scheduled jobs and customize at command job scheduling. The available options are:

OptionDescription
-VPrints the program version number to standard output.
-q [queue]Uses the specified [queue] consisting of a single letter, ranging from a-z and A-Z. The a queue is the default for at and the b queue is the default for batch. Queues with higher letters run with increased niceness.
Submitting a job to a queue with an uppercase letter treats the job as submitted to batch. The batch load average rules apply once it is time to execute the job. Appointing a queue to atq causes it to show only jobs pending in that queue.
-mEmails the user after the job has completed, even if there was no output. Requires a configured email address for the user that scheduled the job.
-f [file]Reads the job from the specified [file] rather than from standard input.
-bAn alias for batch. Schedules jobs and executes them in a batch queue when the system load level average is below 1.5.
-lAn alias for atq. Lists the user's pending jobs. If the user is superuser, lists all users' pending jobs.
-dAn alias for atrm. Deletes the scheduled jobs, identified by their job number.
-vShows the job execution time before reading the job. The time format is Thu Feb 20 14:50:00 1997.
-cCats the specified job, showing its contents in standard command-line output.
-t [time_arg]Schedules a job for the time specified by the [time_arg] argument. The accepted time format is [[CC]YY]MMDDhhmm.

[runtime] Time Expressions

Schedule a job using absolute time expressions or time expressions relative to the time of setting the job.

The available absolute time expressions are:

  • YYMMDDhhmm[.ss]. Specify an abbreviated year, month, day, hour, minute, and optionally seconds.
  • CCYYMMDDhhmm[.ss]. Specify a full year, month, day, hour, minute, and optionally seconds.
  • now. Indicates the current day and time and immediate execution.
  • midnight. Indicates 00:00 AM.
  • noon. Indicating 12:00 PM.
  • teatime. Interpreted as 4 PM.
  • AM. Indicates time before 12:00 PM.
  • PM. Indicates time after 12:00 PM.
  • today. The current day.
  • tomorrow. The day after the current day.

For example, the following command schedules an echo command invocation at 5PM:

echo "hello" | at 5PM

Specify a relative time expression by adding a plus sign (+), the amount, and one of the following:

  • minutes
  • hours
  • days
  • weeks
  • months
  • years

Using time expressions such as tomorrow or tuesday, schedules the jobs on those days at the current time. Further specify the time using the + character.

For example, the following command schedules an echo command invocation five minutes after scheduling the job:

echo "hello" | at now +5 minutes

Environment

The SHELL environment variable value determines which shell (bash, zsh, etc.) the at command uses to execute the job. When scheduling the job, the command warns the user which shell it will use during execution.

Linux at Command Examples

The following section demonstrates useful at command examples.

Schedule a Job Interactively

The at command allows you to schedule a job using the interactive at prompt. Open the interactive prompt using the following syntax:

at [runtime]

The interactive prompt allows you to enter which commands to run at the specified time. The command also prints a warning stating which shell the command will use.

Exit the interactive prompt and save the scheduled job by pressing Ctrl + D. Cancel the job with Ctrl + C.

For example:

Using the at interactive prompt for scheduling a job.

In the example above, we scheduled a job for 5 PM, which opens the interactive prompt. The scheduled job is to execute the echo command, and the utility uses the standard bash shell.

Pipe a Job to at

Schedule a job without the interactive at prompt by piping commands to at and specifying the runtime. Use echo or printf to pass the commands to at.

For example:

echo "command_to_run" | at [runtime]

To schedule an at job that sends the echo command output to a file, use:

echo "hello" >> example.txt | at now

Use the cat command to check if the file exists to make sure the job was executed:

cat example.txt

View a Scheduled Job

The -c option displays the contents of a previously scheduled job. The option is useful if you forget what the job was or want to check the time scheduled.

See the contents of a scheduled at job using the following syntax:

at -c [job_number]

Obtain the job number by running the atq command first to list all pending jobs.

For example:

Showing the contents of a scheduled job in the command line.

In the example above, we first requested a list of all pending jobs to obtain the job numbers and then showed the contents of job 14 in standard output.

List Scheduled Jobs

The at utility allows you to review which jobs are still in the queue and pending execution. There are two ways to see the pending jobs:

  • Run the atq command.
  • Use the -l option.

For example:

at -l
Listing all pending at jobs.

The output shows the pending job number, date, time, year, and queue for the current user. To list pending jobs of all users, run the command with sudo.

sudo atq
Showing the pending at jobs for all users on a system.

In the example above, running atq shows pending jobs only for the current user. Running atq with sudo shows pending jobs for all users.

Send an Email Notification on Execution

The at utility sends email notifications about completed jobs by default. Email notifications require a working email address configured for your user account, and the system needs to be able to send emails.

To instruct at to send email notifications on job completion, even if there is no output, specify the -m option.

For example:

rm example.txt | at -m tomorrow

The command above schedules the job for the current time tomorrow. When completed, the at utility notifies the user using the configured email address for the user bosko.

Execute Jobs Without Notifying

To avoid getting email notifications about completed jobs, specify the -M option when scheduling an at job.

For example:

echo "shutdown -h now" | at -M 00:00
Scheduling an at job and preventing the email notification option.

In the example above, we scheduled a system shutdown for midnight and used the -M option to prevent the utility from sending the email notification.

Remove a Scheduled Job

To remove scheduled at jobs, specify the -r option, or run the atrm command along with the job ID. Obtain the job ID by running atq or utilizing the -l option.

In the following example, we removed job 12:

Removing a scheduled job from the queue.

Restrict Users

The at package utilizes two files to restrict at and batch command access:

  • /etc/at.allow
  • /etc/at.deny

Edit the files to permit only certain users to create, remove, or display pending at jobs. The files are a list of usernames that can or cannot access the at commands. A newline character separates the usernames, and whitespaces are not permitted.

Important: If the /etc/at.allow file exists, only the users listed in it are allowed to use at or batch, and the /etc/at.deny file is ignored. If the /etc/at.allow file doesn't exist, the users listed in /etc/at.deny aren't allowed to use at or batch. The root user can always execute at commands, regardless of the access control files.

Edit the /etc/at.deny file with sudo privileges to deny a user access to at commands. In the following example, we edited the file using the vi editor:

sudo vi /etc/at.deny

After adding a username to the list, running the at command using the restricted user account results in the following message:

Restricting user access to the at command.

Execute Jobs When System Load Permits

The batch command (-b option) schedules an at job for execution when the system load average drops below 1.5. Executing jobs when the system load is low prevents the consumption of all system resources and ensures optimal system operation.

Note: Use the top command to check system load level and resource usage.

For example, the following job executes as soon as the system load drops below 1.5:

batch
echo "Hello world!" >> hello.txt
An example of using the batch command.

Conclusion

This guide showed how to install and use the at command in Linux. Use the at utility to schedule one-time jobs and avoid forgetting to run them later. The utility is an alternative to cron jobs, with simpler syntax and no recurring execution.

For other useful Linux commands, check out our Linux commands cheat sheet, with all important Linux commands in one place.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Crontab Reboot: How to Execute a Job Automatically at Boot
October 28, 2020

Cron jobs are a built-in Linux utility that allow you to automate and schedule recurring tasks. We go over different ways you can schedule jobs to run at system boot.
Read more
Using the Linux free Command
December 29, 2021

The free command in Linux is a utility that helps monitor RAM usage on a system. This guide shows how to use the free command and determine if there are enough resources for running new apps.
Read more
How to Use the ulimit Linux Command
December 9, 2021

The ulimit shell command allows you to view or limit the amount of resources a user can consume. Limiting resources prevents adverse effects in the system when a user or program consumes all the available resources.
Read more
MySQL Events and Event Scheduler Guide
April 27, 2021

MySQL events run on a schedule defined by the user. They automate database maintenance and execute tasks periodically. Learn how to use them in this simple tutorial.
Read more