How to Use the Linux at Command

October 17, 2024

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.
  • Access to the terminal.
  • A user account with sudo privileges.

Install at Command

Depending on your Linux system, the at command may not be pre-installed. Check if at is installed by entering the command name in the terminal:

at
command at not found terminal output

If the utility isn't pre-installed, the output message states Command 'at' not found. Follow the steps below to install at on Ubuntu or Debian:

1. Update the package repository:

sudo apt update
sudo apt update terminal output

2. Install the at command by running:

sudo apt install at
sudo apt install at terminal output

To install at on RHEL-based distributions, run this command:

sudo dnf install at

To use at, enable the 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
sudo systemctl enable --now atd terminal output

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]Emails the user after the job has been completed for the queue, even if there was no output. Requires a configured email address for the user who scheduled the job.
-mEmails the user after the job has been completed, even if there was no output. Requires a configured email address for the user who 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, it 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.

The runtime parameter refers to the specific date and time when you want the scheduled job to be executed. You can provide it in various formats, such as an exact time or a relative time.

Linux at Command and Specifying Times 

Linux at command allows users to specify absolute and relative times for task execution using various formats like exact hours, dates, or intervals from the current time. The following text elaborates on how to specify times with the at command.

Specific Times

Specify the exact time for the at command to run tasks in two formats:

  • 24-hour clock format.
  • 12-hour clock format.

For example, to schedule an echo command at 5 PM, use:

echo "hello" | at 17:00
echo "hello" | at 17:00 terminal output

Alternatively, the following command does the same using the 12-hour format:

echo "hello" | at 5PM
echo "hello" | at 5PM terminal output

Note: Both echo "hello" | at 5PM and echo "hello" | at 5 PM are valid commands to schedule a job at 5 PM using the at command, with the first being a compact format and the second including a space.

If the specified time has already passed, the task is scheduled for the same time the next day.

Customary Times

In addition to specifying exact times, you can schedule at tasks using commonly recognized times, such as:

  • now. Represents the current day and time and immediate execution.
  • midnight. Indicates 00:00 AM.
  • noon. Indicates 12:00 PM.
  • teatime. Represents 4 PM.
  • AM. Indicates time before 12:00 PM.
  • PM. Indicates time after 12:00 PM.
  • today. Represents the current day.
  • tomorrow. Represents the day after the current day.

For example, to schedule tasks at midnight, run:

echo "hello" | at midnight
echo "hello" | at midnight terminal output

Relative Times

Relative times in the at command refer to scheduling tasks based on time intervals from the current moment, such as now + 2 hours or in 3 days. This allows you to specify when tasks should run by indicating a future period rather than an exact time. For example, to schedule the echo command to run in 2 hours from now, run:

echo "hello" | at now + 2 hours
echo "hello" | at now + 2 hours terminal output

Days of the Week

With the at command, schedule tasks for specific days of the week. This allows you to schedule a task for the upcoming occurrence of a specific day without needing to specify an exact date.

For example, to schedule an echo command for the next Monday at 9 AM, run:

echo "hello" | at 9AM next Monday
echo "hello" | at 9AM next Monday terminal output

Dates

With the at command, schedule tasks for specific dates by using formats like MM/DD/YYYY or DD.MM.YYYY. This allows you to specify an exact date and time for task execution.

For example, to schedule an echo command for October 20, 2024, at 10:00 AM, run:

echo "hello" | at 10:00 10/20/2024
echo "hello" | at 10:00 10/20/2024 terminal output

Linux at Command Examples

The at command has plenty of use-case 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. The interactive prompt lets you enter which commands to run at the specified time. The command also prints a warning stating which shell the command uses.

The following syntax opens the interactive prompt using t:

at [runtime]

For example, access the interactive prompt by scheduling a job for 5 PM with :

at 5 PM
at 5 PM terminal output

Schedule the job to execute the echo command using the standard bash shell:

echo "hello"
<EOT>
at echp "hello" <EOT> terminal output

The <EOT> signifies to press Ctrl+D to indicate you have finished entering commands.

Pipe a Job to at

Schedule a job without the interactive at prompt by piping commands to at and specifying the runtime. To use echo to pass the commands to at, follow this syntax:

echo [command_to_run] | at [runtime]

For example, schedule an at job for now. The job sends the echo command output to a file called example.txt. To do so, use:

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

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

cat example.txt
cat example.txt terminal 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, run the following:

at -l
at -l terminal output

The output shows the pending job number, date, time, year, and queue for the current user. The atq command provides the same output:

atq
terminal output for atq

To list the pending jobs of all users, run the command with sudo.

sudo atq

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.

To accomplish that, first obtain the job number by running the atq command to list all pending jobs.

atq
atq terminal output

Next, see the contents of a scheduled at job using the following syntax:

at -c [job_number]

For example, see the contents of a scheduled job number 7 with:

at -c 7
at -c 7 terminal output

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:

echo "Job completed" | at -m now + 1 minute
echo "Job completed" | at -m now + 1 minute terminal output

The command above schedules a job to run in 1 minute and sends an email notification to the user sara when it is complete. After the job finishes, use the mail command to check your mail:

mail
mail terminal output

Execute Jobs Without Notifying

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

For example:

echo "Job completed" | at -M now + 1 minute
Terminal output for echo "Job completed" | at -M now + 1 minute

In the example above, the command is scheduled for +1 minute, and the -M option prevents 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. To do that, first obtain the job ID by running atq or utilizing the -l option:

atq
atq command terminal output

In the following example, remove job 7:

atrm 7

The command has no output, but atq shows that the job is removed:

terminal output for atq command

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 contain 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.

For example, in this case, the /etc/at.allow file does not exist. Edit the /etc/at.deny file with sudo privileges to deny a user access to at commands using a text editor of choice. In the following example, we edited the file using the Vim editor:

sudo vim /etc/at.deny
sudo vim /etc/at.deny terminal output

The example shows the username sara added to the list. After making changes, save and exit the file.

Runn the at command using the restricted user account:

at -l
terminal output for at -l

The output shows the user does not have permission to use the command.

Execute Jobs When System Load Permits

The batch command (or at -b) schedules a task to run when the system's load is low, i.e., when the load average drops below 1.5. This helps prevent the task from using too many resources and keeps the system running smoothly.

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

For example, to execute a job as soon as the system load drops below 1.5, run:

at -b
at-b-terminal-output

To set a command that appends the text "Hello world!" to a file named hello.txt., run:

echo "Hello world!" >> hello.txt
echo "Hello world!" >> hello.txt terminal output

To signal the end of input, run:

<EOT>
<EOT> terminal output

Press Ctrl+D to indicate you have finished entering commands.

Read Commands From a File

The -f option with the at command allows you to schedule a job by reading commands from a file instead of using standard input. For example, create a script named backup.sh in your /home/sara/ directory with:

echo "echo 'Backup started'" > /home/sara/backup.sh

The command outputs the text echo 'Backup started' to a file named backup.sh in the /home/sara/ directory

Schedule the script for 5 PM today with:

at -f /home/sara/backup.sh 5PM
at -f /home/sara/backup.sh 5PM terminal output

Conclusion

This guide explained 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.

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

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
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