How to Use the Command 'at' (with Examples)
The at
command is a scheduling utility found in Unix and Unix-like operating systems that allows users to execute commands at a specified future time. Unlike cron jobs, which are used for recurring tasks, at
is specifically designed for one-time tasks. This command proves indispensable for situations when a task needs to be scheduled just once or at non-regular intervals. When you schedule a command using at
, its output is sent to the user’s mail, providing a convenient way to track the execution outcome. Below are various use cases illustrating how to leverage the at
command effectively.
Use Case 1: Start the atd
daemon
Code:
systemctl start atd
Motivation:
Before scheduling any tasks with the at
command, the atd
daemon needs to be running. The daemon atd
is responsible for executing commands scheduled by the at
command. Starting this daemon ensures that your scheduled tasks will be executed at the specified times.
Explanation:
systemctl
: This is a command used to examine and control the systemd system and service manager.start
: This argument tellssystemctl
to start the specified service.atd
: This refers to the daemon of theat
command, responsible for carrying out scheduled tasks.
Example Output:
There might be no direct output on successful execution of this command. You can confirm that atd
has started by checking the status:
systemctl status atd
Use Case 2: Create Commands Interactively and Execute Them in 5 Minutes
Code:
at now + 5 minutes
Motivation:
Scheduling a task to execute after a short delay can be useful for troubleshooting or experimenting with different configurations, allowing you just enough time to configure or set up prerequisites for the task’s execution. It also provides a way to perform tasks that should happen once without manual intervention.
Explanation:
at
: Starts the command scheduling interface.now
: Indicates the current time, serving as the base point for scheduling the command.+ 5 minutes
: Defines a relative time, scheduling the command to execute five minutes from the current time.
Example Output:
The system waits for input. After pressing <Ctrl> + D
, you’ll receive a job number indicating the task is scheduled:
job 1 at Fri Aug 19 16:55:00 2023
Use Case 3: Create Commands Interactively and Execute Them at a Specific Time
Code:
at 15:00
Motivation:
Scheduling tasks for specific times can be essential for maintenance tasks that should minimize disruption. For example, you may want specific data processing jobs to run after business hours to not impact server performance.
Explanation:
at
: Initiates the scheduler interface.15:00
: Specifies an exact time (in 24-hour format) for the command execution.
Example Output:
After providing input and completing it with <Ctrl> + D
, you’ll receive:
job 2 at Fri Aug 19 15:00:00 2023
Use Case 4: Execute a Command from stdin
at 10:00 AM Today
Code:
echo "command" | at 1000
Motivation:
This use case is beneficial for quick, one-liner commands that need to be executed at a certain time without opening an interactive session with at
. It’s particularly useful for users familiar with scripting and who want to pass commands directly via pipes.
Explanation:
echo "command"
: Prints the command you wish to schedule. Replace “command” with your actual command.|
: Pipes the output of the echo command to another command.at 1000
: Schedules the piped command to run at 10:00 AM today.
Example Output:
job 3 at Fri Aug 19 10:00:00 2023
Use Case 5: Execute Commands from a Given File Next Tuesday
Code:
at -f path/to/file 9:30 PM Tue
Motivation:
Executing multiple commands from a file on a specific day can be useful for batch processing tasks or deploying pre-written event scripts. This approach streamlines workflow by keeping all tasks encapsulated in a script file, ideal for environments where tasks are repeatedly needed.
Explanation:
at
: Instigates the scheduling feature.-f path/to/file
: Specifies a file containing commands to execute. Replace “path/to/file” with your actual file path.9:30 PM
: Designates the time for the task on Tuesday.Tue
: Specifies the day of the week to execute the command(s).
Example Output:
Once the command is entered, output similar to this appears:
job 4 at Tue Aug 23 21:30:00 2023
Conclusion:
The at
command provides a straightforward method for scheduling one-time jobs on Unix-like systems. Its versatility allows for scheduling simple tasks or complex batch processes, adapting to various administrative tasks and user needs. By understanding different ways to implement at
, users can enhance their automation capabilities and optimize their workflow scheduling.