At Command in Linux
Updated on
•7 min read
at
is a command-line utility that allows you to schedule various commands and scripts to be executed automatically at a specific time and date. Jobs created with at
are executed only once, making it an excellent tool for managing one-time tasks that need to be completed at an exact time.
In this article, we will explain how to use at
and its companion utilities batch
, atq
, atrm
to view, delete, and create jobs to be executed at a later time.
Installing at
Depending on the distribution, at
may or may not be present on your Linux system.
If the command is not installed, you can easily install it using your distribution’s package manager.
Install
at
on Ubuntu and Debiansudo apt update
sudo apt install at
Install
at
on CentOS and Fedorasudo yum install at
Once the package is installed make sure atd
, the scheduling daemon, is running and set to start on boot:
sudo systemctl enable --now atd
How to Use the at
Command
The simplified syntax for the at
command is as follows:
at [OPTION...] runtime
The at
command takes the date and time (runtime
) when you want to execute the job as a command-line argument, and you enter the command to be executed from the standard input.
For instance, let’s create a job that will be executed at 9:00 am:
at 09:00
After you press the Enter
key, you will be presented with the at
command prompt, which usually begins with at>
. Additionally, there will be a warning message displayed that informs you about the shell in which the command will be executed:
warning: commands will be executed using /bin/sh
at>
Enter one or more command you want to execute:
tar -xf /home/linuxize/file.tar.gz
When you’re done entering the commands, press Ctrl-D
to exit the prompt and save the job:
at> <EOT>
job 4 at Tue May 5 09:00:00 2020
The command will display the job number and the execution time and date.
There are also other ways to pass the command you want to run, besides entering the command in the at
prompt. One way is to use the echo
command and pipe the command to at
:
echo "command_to_be_run" | at 09:00
Another option is to use Here document :
at 09:00 <<END
command_to_be_run
END
To read the commands from a file instead of the standard input, invoke the command with -f
option followed by the path to the file. For example, to create a job that will run the script /home/linuxize/script.sh
:
at 09:00 -f /home/linuxize/script.sh
By default, if the command produces output, at
will send an email including the output to the user once the job is completed. Invoke at
with the -M
option to suppress the email notification:
at 09:00 -M
Use the -m
to send an email even if there is no output:
at 09:00 -m
batch
Command
batch
or its alias at -b
schedules jobs and executes them in a batch queue when the system load level permits. By default, the jobs are executed when the system load average is below 1.5. The value of the load can be specified when invoking the atd
daemon. If the system load average is higher than then specified one, the jobs will wait in the queue.
To create a job with batch
, pass the commands you want to execute:
echo "command_to_be_run" | batch
Specifying the Execution Time
The at
utility accepts a wide range of time specifications. You can specify the time, date, and increment from the current time:
Time - To specify a time, use the
HH:MM
orHHMM
form. If you prefer to use the 12-hour clock format, you can add “am” or “pm” after the time. For example, 10:30am or 6:45pm. You can also use strings likenow
,midnight
,noon
, orteatime
(16:00). If a task or event is scheduled for a time that has already passed, it will be automatically rescheduled for the same time on the next day.Date - The command allows you to schedule job execution on a given date. The date can be specified using the month name followed by the day and an optional year. Alternatively, you can use strings like ’today’, ’tomorrow’, or weekday to indicate the date. The date can be also indicated using the
MMDD[CC]YY
,MM/DD/[CC]YY
,DD.MM.[CC]YY
or[CC]YY-MM-DD
formats.Increment -
at
also accepts increments in thenow + count time-unit
format, wherecount
is a number andtime-unit
can be one of the following strings:minutes
,hours
,days
, orweeks
.
Time, date, and increment can be combined. Here are a few examples:
Schedule a job for the coming Sunday at a time ten minutes later than the current time:
at sunday +10 minutes
Schedule a job to run at 1 pm two days from now:
at 1pm + 2 days
Schedule a job to run at 12:30 Oct 21 2020:
at 12:30 102120
Schedule a job to run one hour from now:
at now +1 hours
You can also specify a time and date in the [[CC]YY]MMDDhhmm[.ss]
using the -t
option. Here is an example:
at -t 202005111321.32
Specifying Queue
By default, the jobs created with at
are scheduled in the queue named a
, and jobs created with batch
are scheduled in the b
queue.
Queries can have a name from a
to z
and A
to Z
. Queues with lower letters run with lower niceness, which means they have priority over those with higher letters.
To specify the queue for a job, use the -q
option followed by the desired queue name. For instance, if you want to set a job in the ‘L’ queue, you should execute the command with -q L
option:
at monday +2 hours -q L
Listing Pending Jobs
To list the user’s pending jobs, run the atq
or at -l
command:
atq
The output will list all jobs, one per line. Each line includes the job number, date, time, queue letter, and username.
9 Tue May 5 12:22:00 2023 a linuxize
12 Wed Oct 21 12:30:00 2023 a linuxize
15 Tue May 5 09:00:00 2023 a linuxize
6 Tue May 5 09:00:00 2023 a linuxize
13 Mon May 4 23:08:00 2023 a linuxize
11 Wed Jul 1 10:00:00 2023 a linuxize
4 Tue May 5 09:00:00 2023 a linuxize
When atq
is invoked as an administrative user, it will list the pending jobs of all users.
Removing Pending Jobs
To remove a pending job invoke the atrm
or at -r
command followed by the job number. For example, to remove the job with number nine, you would run:
atrm 9
Restricting Users
The /etc/at.deny
and /etc/at.allow
files allow you to control which users can create jobs with at
or batch
command. The files contain a list of usernames, one user name per line.
By default, only the /etc/at.deny
file exists
and is empty, meaning that all users can use the at
command. If you want to deny permission to a specific user, add the username to this file.
If the /etc/at.allow
file exists only the users who are listed in this file can use the at
command.
If neither of the files exists, only the users with administrative privileges can use the at
command.
Conclusion
The at
command is a useful utility that allows you to schedule commands to be executed at a later time. Unlike the crontab
command, which executes commands in a recurring fashion, the at
command runs commands only once.
To learn more about the various options available with the at
command, you can type man at
in your terminal. This will bring up the manual page that lists all the available options with detailed explanations.
If you have any further questions or need assistance in using the at
command, please feel free to leave a comment.