Linux at Command
at is a powerful task-scheduling tool in Linux that lets users run one-time tasks at a specified time, rather than periodically like cron that way.
The difference between cron Compared toat Better suited to the following scenarios:
- Tasks that only need to run once
- Tasks that run at a specific point in the future
- Temporary system maintenance operations
Installation and Basic Usage
Check the installation
Most Linux distributions have it installed by default at, which you can check with the following command:
which at
If it is not installed, you can install it with a package manager:
Example
Debian/Ubuntu
sudo
apt-get install
at
CentOS/RHEL
sudo
yum install
at
Start the at Service
After installation, you need to start atd the daemon:
Example
sudo
systemctl start atd
sudo
systemctl
enable
atd
Enable startup on boot
Command Syntax and Parameters
Basic Syntax
at [选项] 时间
Common options
| Option | Description |
|---|---|
-f 文件 |
Read commands from a specified file instead of standard input |
-m |
Send an email to the user after the task completes |
-l |
List pending tasks (same as atq) |
-d 任务ID |
Delete a specified task (same as atrm) |
-v |
Show the time when a task will be executed |
Time Formats
at Multiple time formats are supported:
-
Absolute time
HH:MM (such as 14:30)YYYY-MM-DD (such as 2023-12-25)HH:MM YYYY-MM-DD-
Relative time
now + 数量 单位 (such as now + 2 hours)-
Special Keywords
noon (noon, 12 o'clock)midnight (midnight)teatime (4 p.m.)tomorrow (same time tomorrow)Usage examples
Example 1: Basic Usage
Example
$ at
15
:
30
warning: commands will be executed using
/
bin
/
sh
at
echo
"Hello at command"
~
/
at_test.txt
at
Press Ctrl+D to end the input
job
1
at Tue Jul
18
15
:
30
:00
2023
Example 2: Reading Commands from a File
Create a script file myscript.sh:
Example
#!/bin/bash
echo
"This is a test"
/
tmp
/
at_log
date
/
tmp
/
at_log
Then use:
at -f myscript.sh now + 5 minutes
Example 3: Viewing and Deleting Tasks
Example
$ atq
or at -l
1
Tue Jul
18
15
:
30
:00
2023
a username
$ atrm
1
or at -d 1
Advanced usage
Set environment variables
Example
$ at
16
:00
at
export
MY_VAR
=
"test"
at
echo
$MY_VAR
~
/
env_test.txt
at
Execute complex commands
Example
$ at
17
:00 today
at
cd
/
var
/
log
&&
grep
"error"
syslog
~
/
error_log.txt
at
Use a here document
Example
at
18
:00
<< 'EOF'
#!/bin/bash
echo "Starting backup..."
tar -czf ~/backup-$(date +%F).tar.gz ~/Documents
EOF
Notes
-
Permission Control
/etc/at.allow and /etc/at.deny Files Control User Access Permissionsat.allow exists, only the listed users can use it atat.allow does not exist, check at.deny, in which the listed users are denied-
Output handling
-m option forces sending an email even if there is no output-
Environment Differences
at The environment at execution time may differ from the interactive shell-
Error handling
atd Check whether the service is running:systemctl status atdjournalctl -u atdPractical Exercise
- Create a task that appends the current date and time after 5 minutes to
~/time_log.txtfile - Write a script to back up the
/etcdirectory, and useatto schedule it for 2:00 AM tomorrow - List all currently pending
atjobs, then delete one of them
Summary
at The command is a powerful tool for managing one-time scheduled tasks in Linux. Through this article, you should have mastered:
atBasic concepts and installation- How to specify various time formats
- How to create, view, and delete jobs
- Advanced usage and precautions
For tasks that need to run periodically, you can consider using cron, but for one-time tasks,at is undoubtedly the best choice.
Linux Command Reference