Scheduling Commands and Scripts to Run Automatically at Specific Times and Dates Using the ‘at’ Command in Linux

Scheduling Commands and Scripts to Run Automatically at Specific Times and Dates Using the 'at' Command in Linux

<span>at</span> is a command-line tool used to automatically execute various commands or scripts at a specified time and date. Tasks created with <span>at</span> are executed only once, making it an ideal tool for managing one-time tasks at precise time points.

This article will introduce how to use <span>at</span> along with its companion tools <span>batch</span>, <span>atq</span>, and <span>atrm</span> to view, delete, and create delayed execution tasks.

1. Installing at

Depending on the Linux distribution, <span>at</span> may not be pre-installed. If it is not installed, you can quickly install it using the package manager:

Installation on Ubuntu/Debian systems:

$ sudo apt update 
$ sudo apt install at

Installation on CentOS/Fedora systems:

$ sudo yum install at

After installation, ensure that the scheduling daemon <span>atd</span> is running properly and set to start on boot:

$ sudo systemctl enable --now atd

2. Basic Usage of the at Command

<span>at</span> command has a simplified syntax as follows:

$ at [options...] execution_time

This command requires specifying the task execution time (<span>runtime</span>) in the parameters and entering the command to be executed via standard input.

For example, to create a task that executes at 9 AM:

$ at 09:00

After pressing Enter, you will enter the <span>at</span> interactive prompt (usually displayed as <span>at></span>), along with a prompt for execution environment information:

warning: commands will be executed using /bin/sh
at>

Input the command to be executed (multiple lines are allowed):

at> tar -xf /home/haopython/file.tar.gz

After finishing the input, press <span>Ctrl-D</span> to save the task, and the system will return the task number and execution time:

at> <EOT>
job 4 at Tue May  5 09:00:00 2025

3. Other Command Input Methods

1) Piping Parameters: Pass commands using <span>echo</span>:

$ echo "command_to_be_run" | at 09:00

2) Here Document:

$ at 09:00 <<END
command_to_be_run
END

3) Reading from a File: Use <span>-f</span> to specify the script path:

$ at 09:00 -f /home/haopython/script.sh

4) Email Notification Control

By default, the task output will be sent to the user via email. Use <span>-M</span> to disable notifications:

$ at 09:00 -M

Use <span>-m</span> to force sending an empty output email:

$ at 09:00 -m

4. Batch Command

<span>batch</span> (or alias <span>at -b</span>) will execute batch tasks when the system load is below a threshold. The default threshold is 1.5 (which can be adjusted via the <span>atd</span> daemon). If the system load exceeds the threshold, the tasks will be queued.

Example usage:

$ echo "command_to_be_run" | batch

5. Time Parameter Specifications

<span>at</span> supports flexible time formats, allowing the combination of the following elements:

1) Time: Supports <span>HH:MM</span> or <span>HHMM</span> formats, with 12-hour format allowing the addition of <span>am/pm</span> (e.g., <span>10:30am</span>). Keywords include: <span>now</span> (immediate), <span>midnight</span> (midnight), <span>noon</span> (noon), <span>teatime</span> (4 PM). If the specified time has passed, it will be postponed to the same time the next day.

2) Date:

  • Month name + date (optional year), e.g., <span>Oct 21 2020</span>
  • Keywords: <span>today</span> (today), <span>tomorrow</span> (tomorrow), <span>weekday</span> (weekday)
  • Formats: <span>MMDD[CC]YY</span>, <span>MM/DD/[CC]YY</span>, <span>DD.MM.[CC]YY</span> or <span>[CC]YY-MM-DD</span>

3) Time Increment:<span>now + number time_unit</span> (units: <span>minutes</span>/<span>hours</span>/<span>days</span>/<span>weeks</span>)

Here are some combination examples:

  • Execute 10 minutes after the current time (next Sunday):
$ at sunday +10 minutes
  • Execute at 1 PM two days later:
$ at 1pm + 2 days
  • Specify execution at 12:30 on October 21, 2025:
$ at 12:30 102125
  • Execute one hour later:
$ at now +1 hours
  • Use <span>-t</span> to specify the <span>[[CC]YY]MMDDhhmm[.ss]</span> format:
$ at -t 202505111321.32

6. Task Queue Management

  • The default queue: <span>at</span> tasks use the <span>a</span> queue, while <span>batch</span> tasks use the <span>b</span> queue.
  • Queue priority: The lower the alphabetical order of the queue (e.g., <span>a</span>), the higher the priority (lower nice value).
  • Example of specifying a queue (using <span>L</span> queue):
$ at monday +2 hours -q L

7. Viewing Pending Tasks

Execute <span>atq</span> or <span>at -l</span> to list the current user’s tasks:

$ atq

Output format: task number execution time queue letter username

9   Mon May  5 12:22:00 2025 a haopython
12  Tue Oct 21 12:30:00 2025 a haopython
...(略)...

When executed by an administrator, all user tasks will be displayed.

8. Deleting Pending Tasks

Use <span>atrm</span> or <span>at -r</span> + task number to delete:

$ atrm 9

9. User Permission Control

Limit user access to <span>at/batch</span><span> using the following files:</span>

  • <span>/etc/at.deny</span>: Blacklist users (default exists and is empty, allowing all users)
  • <span>/etc/at.allow</span>: Whitelist users (if it exists, only users in the list are allowed)

When both files do not exist, only administrators can use <span>at</span>.

10. Typical Application Scenarios

# Automatically package logs after work (today at 17:30)
$ at 17:30 <<EOF
tar -czf /var/log/nginx/$(date +\%F).tar.gz /var/log/nginx/*.log
EOF

# Run data cleaning during low load (execute when the system is idle)
$ echo "python3 /scripts/data_clean.py" | batch

11. Summary

As one of the core tools for task scheduling in Linux, <span>at</span> focuses on the precise management of one-time delayed tasks, complementing the <span>crontab</span> for periodic tasks. The typical division of labor between the two is as follows:

Tool Applicable Scenarios Execution Characteristics Management Complexity
<span>at</span> Scheduled shutdowns / Temporary backups / Late-night batch processing Single execution Low (no ongoing maintenance)
<span>crontab</span> Log rotation / Regular synchronization / Daily reports Cyclic execution High (requires long-term maintenance)

Scheduling Commands and Scripts to Run Automatically at Specific Times and Dates Using the 'at' Command in Linux

Leave a Comment