Linux at Command
atis a powerful scheduled task scheduling tool in the Linux system, allowing users to execute one-time tasks at a specified time, rather than likecronexecute periodically like that.
andcronCompared with,atit is more suitable for the following scenarios:
- Tasks that need to be executed only once
- Tasks that run at a specific point in time in the future
- Temporary system maintenance operations
Installation and Basic Usage
Check Installation
Most Linux distributions have it installed by defaultat, you can check with the following command:
which at
If it is not installed, you can use the package manager to install it:
Example
sudo apt-get install at
# CentOS/RHEL
sudo yum install at
Start the at Service
After installation, you need to startatdthe daemon:
Example
sudo systemctl enable atd # Enable auto-start on boot
Command Syntax and Parameters
Basic Syntax
at [选项] 时间
Common Options
| Option | Description |
|---|---|
-f 文件 |
Read commands from the specified file instead of standard input |
-m |
Send email to the user after the task completes |
-l |
List tasks waiting to be executed (same asatq) |
-d 任务ID |
Delete the specified task (same asatrm) |
-v |
Display the time when the task will be executed |
Time Format
atSupports multiple time formats:
-
Absolute time
HH:MM(e.g., 14:30)YYYY-MM-DD(e.g., 2023-12-25)- Combined format
HH:MM YYYY-MM-DD
-
Relative time
now + 数量 单位(such asnow + 2 hours)- Units can be: minutes, hours, days, weeks
-
Special keywords
noon(12 noon)midnight(midnight)teatime(4 PM)tomorrow(same time tomorrow)
Usage Examples
Example 1: Basic Usage
Example
warning: commands will be executed using /bin/sh
at> echo "Hello at command" > ~/at_test.txt
at> # Press Ctrl+D to end input
job 1 at Tue Jul 18 15:30:00 2023
Example 2: Read Commands from a File
Create a script filemyscript.sh:
Example
echo "This is a test" >> /tmp/at_log
date >> /tmp/at_log
Then use:
at -f myscript.sh now + 5 minutes
Example 3: View and Delete Tasks
Example
1 Tue Jul 18 15:30:00 2023 a username
$ atrm 1 # or at -d 1
Advanced Usage
Setting Environment Variables
Example
at> export MY_VAR="test"
at> echo $MY_VAR > ~/env_test.txt
at>
Executing Complex Commands
Example
at> cd /var/log && grep "error" syslog > ~/error_log.txt
at>
Using Here Document
Example
#!/bin/bash
echo "Starting backup..."
tar -czf ~/backup-$(date +%F).tar.gz ~/Documents
EOF
Notes
-
Permission Control
/etc/at.allowand/etc/at.denyThe files control user access permissions- If
at.allowexists, only listed users can useat - If
at.allowdoes not exist, checkat.deny, users listed in it are forbidden
-
Output Handling
- By default, the output of the command is sent to the user by email
- Use
-moption to force sending email even if there is no output
-
Environment Differences
atThe environment at execution time may differ from the interactive shell- Important environment variables should be explicitly set in the command
-
Error Handling
- Check
atdwhether the service is running:systemctl status atd - View system logs to get error information:
journalctl -u atd
- Check
Practical Exercises
- Create a task that appends the current date and time to
~/time_log.txtfile - Write a script to back up
/etcdirectory, and useatschedule it to execute at 2 AM tomorrow - List all currently pending
attasks, then delete one of them
Summary
atThe command is a powerful tool in the Linux system for managing one-time scheduled tasks. Through this article, you should have mastered:
atIts basic concepts and installation methods- Methods for specifying various time formats
- Operations for creating, viewing, and deleting tasks
- Advanced usage and precautions
For tasks that need to be executed periodically, consider usingcron, but for one-time tasks,atis undoubtedly the best choice.
Other Extensions
Linux Command Reference