Schedule One-Time Tasks with the at Command on Linux
The at command in Linux is used to schedule tasks similarly to cron, but in a simplified and non-recurring manner.
Its main purpose is to trigger tasks that should be executed only once.
Its format for scheduling tasks is more user-friendly and closer to human-readable notation.
One characteristic of this program is that it executes tasks even if they have passed their scheduled time. It is useful on computers that are frequently shut down or when power interruptions occur.
Possible date and time notations can be entered in the following formats:
- HH:MM: HH for hour and MM for minute;
- midnight: for midnight;
- noon: for noon;
- teatime: for 16:00;
- MMDDYY: MM for month, DD for day and YY for year;
- MM/DD/YY;
- DD.MM.YY;
- + num unit: Where num is the number of time units. Units can be minutes, days, hours and weeks.
The most common options are:
- -l: Lists all tasks scheduled by at;
- -d N: Deletes a specific task identified by number N;
- -f file: Specifies the file containing the commands to be executed.
See the examples:
Executes the commands specified in the comandos.txt file at tea time at 16:00 hours.
# at -f comandos.txt teatime
Executes the commands specified in the comandos.txt file in 3 minutes.
# at -f comandos.txt +3 minutes
Executes the commands specified in the comandos.txt file 3 hours from now on tomorrow.
# at -f comandos.txt tomorrow +3 hours
Another way to use at is to directly type the commands to be executed at its command prompt:
# at now + 2 minutesat> w at> Ctrl D
job 1 at Fri Oct 18 14:25:00 2019
In this example, at will open an "at>" command prompt to type commands one per line. The "w" command, which shows connected users, will be executed within 2 minutes by at.
To exit the at prompt, press Control-D.
The newly created job can be viewed with the "atq" command:
# atq1 Fri Oct 18 14:25:00 2019 a root
The output of the commands executed by at is sent by email to the user who scheduled the task:
From [email protected] Fri Oct 18 14:33:00 2019
Subject: Output from your job 2
To: [email protected]
Date: Fri, 18 Oct 2019 14:33:00 +0000 (UTC)
14:33:00 up 3 days, 11:49, 1 user, load average: 0.04, 0.13, 0.11
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
uira pts/0 ip-10-8-0-6 14:14 9.00s 0.08s 0.01s sshd: uira[priv]
atq
The atq command lists the user's pending jobs, and in the case of root, lists all jobs. The command outputs the job number, date, time, queue and user.
Example:
$ atq4 2015-05-11 19:00 uribeiro
atrm
The atrm command removes a job from the task queue, identified by the job number.
Example:
$ atrm 4
All scheduled tasks are stored in files inside the /var/spool/at/ directory.
at Permissions
The at command also has two files responsible for access security to the scheduler:
- /etc/at.allow
- /etc/at.deny
If the at.allow file exists, a specific user must have their login listed in its contents to have explicit permissions to use at.
If at.deny exists, the logins listed in its contents are prohibited from using at, and all other users are allowed.
If neither file exists, the use of at will be allowed only for the superuser.
If the at.deny file is empty, at will be allowed for all users.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.