Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
The date command is used to view the current time, customize the display of the time, and modify the system time. In this article we will talk about the delayed execution command at, which we can use to set the time for the delayed execution of a command.
First, environmental preparation
Not all Linux distributions support using the AT command.
If you are using Ubuntu, use the following command line to install this command.
$sudo apt install at
Copy the code
If you are using Centos, please use the following command line to install this command.
$yum install -y <at with version>Copy the code
Check if the installation is working properly!
at 14:30
Copy the code
The console output is:
A warning message appears on the terminal, which means that you need to provide a command to be executed at 14:30. This indicates that the AT command was successfully installed.
Second, the use of
2.1 Basic Usage
An introduction to
If we want to create the file at the specified time, we can enter the command to create the file as shown in the diagram.
Now that the delayed command has been entered, you need to exit and save the Settings. You can use the CRTL +D key combination to exit. EOF, which stands for End Of Transmission, is displayed.
The meaning of the content is as follows:
- Job – Indicates that a task is created
- 3 – For mission 3
- At – means “at…” Moment”
- Sat Sep 25 14:32:2021 – Indicates the execution time of the command, accurate to the second
When the time is up, specific commands will be executed. Check the file directory, indeed successfully executed.
Complete usage
Execute orders at 14:32 tomorrow
$ at 14:32 tomorrow
Copy the code
September 27, 2021 14:32 Execution order
$ at 14:32 09/27/2021
Copy the code
Time interval after execution
In addition to specifying a time, you can also specify an interval after execution.
$at now +30 minutesCopy the code
Interval unit: minutes hours days Weeks Months Years
2.2 Managing the View Function
After a delay command is executed, it will become a task, waiting for the set time to execute, so how to manage and view these delayed tasks?
Simply, each time you set a command to delay execution, you get a job number that you can use to manage tasks.
Atq command
The ATq command can list the AT delayed tasks that are waiting to be executed. Q stands for queue, that is, queue, indicating the task queue of the AT command. The usage method is shown in the figure.
atrm
The atrm command is used to delete the AT delayed tasks that are waiting to be executed. Rm stands for remove, followed by a job number, as shown in the following figure.
Tasks that are delayed on the graph are deleted and therefore will not be executed again.
Three, extension,
Through the above use, the AT command does achieve the effect of delaying the execution of the command, but it should be noted that the AT command can only make the program that needs to be delayed to execute only once. To achieve the effect of periodic repeated execution, please use another powerful command crontab.