
When it comes to scheduling tasks in a Linux environment, system administrators and developers often use the cron command for recurring tasks. However, there is another powerful tool for scheduling one-time jobs, known as the at command. This article will provide an in-depth exploration of the at command, including its syntax, usage examples, and best practices.
The at command is a versatile utility that allows users to schedule a command or script to be executed at a specified time in the future. It is particularly useful for running one-time jobs, such as maintenance tasks, backups, or system updates, without requiring manual intervention. The at command reads the commands to be executed from standard input or from a file and schedules them accordingly.
Most Linux distributions come with the at command pre-installed. However, if it is not present on your system, you can install it using the package manager for your distribution. For Debian-based distributions, use the following command:
sudo apt-get install at
For Red Hat-based distributions, use this command:
sudo yum install at
The basic syntax of the at command is as follows:
at [OPTIONS] TIME
The available options for the at command include:
-f: Specifies a file containing the commands to be executed.-t: Specifies the time at which to run the commands using a Unix timestamp.-m: Sends an email to the user when the job has completed.-q: Specifies a queue in which to place the job.To schedule a one-time job, simply provide the desired time for execution. The at command supports various time formats, such as:
For example, to schedule a one-time job to create a file containing "Hello, World!" in the /tmp directory after one hour, use the following command:
echo "echo 'Hello, World!' > /tmp/hello\_world.txt" | at now + 1 hour
Alternatively, you can schedule the command as below:
at now + 1 hour echo 'Hello, World!' > /tmp/hello\_world.txt
Press CTRL + D to exit from the at command terminal.
To list all scheduled jobs for the current user, use the "atq" command: