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.
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
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
2. Install the at
command by running:
sudo apt install at
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
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:
Option | Description |
---|---|
-V | Prints 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. |
-m | Emails 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. |
-b | An alias for batch . Schedules jobs and executes them in a batch queue when the system load level average is below 1.5. |
-l | An alias for atq . Lists the user's pending jobs. If the user is superuser, it lists all users' pending jobs. |
-d | An alias for atrm . Deletes the scheduled jobs, identified by their job number. |
-v | Shows the job execution time before reading the job. The time format is Thu Feb 20 14:50:00 1997 . |
-c | Cats 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
Alternatively, the following command does the same using the 12-hour format:
echo "hello" | at 5PM
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
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
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
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
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
Schedule the job to execute the echo
command using the standard bash shell:
echo "hello"
<EOT>
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
Use the cat command to check if the file exists to make sure the job was executed:
cat example.txt
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
The output shows the pending job number, date, time, year, and queue for the current user. The atq
command provides the same output:
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
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
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
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
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
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
In the following example, remove job 7:
atrm 7
The command has no output, but atq
shows that the job is removed:
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
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
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
To set a command that appends the text "Hello world!" to a file named hello.txt., run:
echo "Hello world!" >> hello.txt
To signal the end of input, run:
<EOT>
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
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.