Linux at Command

Linux 命令大全Linux Command Reference


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

# Debian/Ubuntu
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 start atd
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:

  1. Absolute time

    • HH:MM(e.g., 14:30)
    • YYYY-MM-DD(e.g., 2023-12-25)
    • Combined formatHH:MM YYYY-MM-DD
  2. Relative time

    • now + 数量 单位(such asnow + 2 hours)
    • Units can be: minutes, hours, days, weeks
  3. Special keywords

    • noon(12 noon)
    • midnight(midnight)
    • teatime(4 PM)
    • 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 input
job 1 at Tue Jul 18 15:30:00 2023

Example 2: Read Commands from a File

Create a script filemyscript.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: View and Delete Tasks

Example

$ atq  # or at -l
1       Tue Jul 18 15:30:00 2023 a username

$ atrm 1  # or at -d 1

Advanced Usage

Setting Environment Variables

Example

$ at 16:00
at> export MY_VAR="test"
at> echo $MY_VAR > ~/env_test.txt
at>

Executing Complex Commands

Example

$ at 17:00 today
at> cd /var/log && grep "error" syslog > ~/error_log.txt
at>

Using Here Document

Example

at 18:00 << 'EOF'
#!/bin/bash
echo "Starting backup..."
tar -czf ~/backup-$(date +%F).tar.gz ~/Documents
EOF


Notes

  1. Permission Control

    • /etc/at.allowand/etc/at.denyThe files control user access permissions
    • Ifat.allowexists, only listed users can useat
    • Ifat.allowdoes not exist, checkat.deny, users listed in it are forbidden
  2. 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
  3. Environment Differences

    • atThe environment at execution time may differ from the interactive shell
    • Important environment variables should be explicitly set in the command
  4. Error Handling

    • Checkatdwhether the service is running:systemctl status atd
    • View system logs to get error information:journalctl -u atd

Practical Exercises

  1. Create a task that appends the current date and time to~/time_log.txtfile
  2. Write a script to back up/etcdirectory, and useatschedule it to execute at 2 AM tomorrow
  3. List all currently pendingattasks, 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.


Linux 命令大全Linux Command Reference

Other Extensions