How to use the command "at" (with examples)
The at
command is used to execute commands once at a later time. It requires the atd
(or atrun
) service to be running for the actual execution of commands.
Use case 1: Execute commands from stdin
in 5 minutes
Code:
at now + 5 minutes
Motivation: This use case allows you to schedule the execution of commands from the standard input in 5 minutes. It can be useful when you want to delay the execution of a command.
Explanation:
at
: The command itself.now + 5 minutes
: Specifies that the command should be executed 5 minutes from the current time.
Example output:
warning: commands will be executed using /bin/sh
at> echo "Hello, world!"
at> <EOT>
job 1 at Tue Jan 4 12:05:00 2022
In this example, the command echo "Hello, world!"
was queued to be executed in 5 minutes and a job number (1
) is assigned to it.
Use case 2: Execute a command from stdin
at 10:00 AM today
Code:
echo "./make_db_backup.sh" | at 1000
Motivation: This use case allows you to schedule the execution of a specific command at a specified time. It can be used to automate tasks, such as backups, that need to be performed at a specific time.
Explanation:
echo "./make_db_backup.sh" | at 1000
: The commandecho "./make_db_backup.sh"
will output the command to be executed (./make_db_backup.sh
) and the pipe (|
) sends the output as the standard input to theat
command.1000
specifies the time of execution as 10:00 AM.
Example output:
warning: commands will be executed using /bin/sh
job 2 at Wed Jan 5 10:00:00 2022
In this example, the command ./make_db_backup.sh
was scheduled to be executed at 10:00 AM today and a job number (2
) is assigned to it.
Use case 3: Execute commands from a given file next Tuesday
Code:
at -f path/to/file 9:30 PM Tue
Motivation: This use case allows you to schedule the execution of commands stored in a file at a specific time. It can be useful when you have a set of commands that need to be executed together.
Explanation:
at
: The command itself.-f path/to/file
: Specifies the file that contains the commands to be executed.9:30 PM Tue
: Specifies the time of execution as 9:30 PM on the next Tuesday.
Example output:
warning: commands will be executed using /bin/sh
job 3 at Tue Jan 11 21:30:00 2022
In this example, the commands from the file specified by path/to/file
will be executed at 9:30 PM on the next Tuesday and a job number (3
) is assigned to it.
Conclusion:
The at
command provides a convenient way to schedule the execution of commands at a later time. It can be used to automate tasks, delay command execution, or execute a sequence of commands stored in a file. By understanding the different use cases and the options available, you can effectively use the at
command in your daily workflow.