2

Hello everyone, this is Liang Xu.

In life, we have too many scenes where we need to use alarm clocks, such as waking up at 7 in the morning, meeting at 4 in the afternoon, shopping at 8 in the evening, and so on.

In the Linux system, we also have similar requirements. For example, we want to upload files to the server at 1:00 in the morning, or check the system status at 10 in the evening, and so on.

But we can't stay in front of the computer all the time. After all, we also need to get off work/sleep and stay with our girlfriends (new one is fine). And even during work hours, manual operations are required if the time comes, which is too inefficient.

at command was born for this need. Using the at command, you can automatically complete the task you set at a specific time, or it can be automated, which is very convenient and fast!

Install at command

Generally speaking, most Linux distributions have already installed the at command. If you are not sure, you can use the at -V command to check. If there is a version return, it means you can use the at command normally.

$ at -V
at version 3.1.13

You may also need to start the at daemon, called atd . On most Linux systems, you can use the systemctl command to enable the atd service and set them to start automatically from now on:

$ sudo systemctl enable --now atd

Basic usage of at command

  • Syntax
at [选项] [日期时间]
  • option
-f:指定包含具体指令的任务文件
-q:指定新任务的队列名称
-l:显示待执行任务的列表
-d:删除指定的待执行任务
-m:任务执行完成后向用户发送 E-mail
  • parameter
日期时间:指定任务执行的日期时间

Use the at command to schedule tasks interactively

Think about it, how do we usually use the alarm clock tool? First, we click the plus sign on the alarm clock software, then set the time, and then enter the purpose of the alarm clock in the remarks.

The same is true of using the at command to schedule tasks interactively. First you enter the at command, and then follow the specific time.

After pressing Enter, you will enter the interactive interface, and then you will enter the specific things (commands) to be done, and finally end the input with <EOT>, and the task will be set.

For example, if you want to write hello world at-test.txt document at 11:20 in the morning, you can enter it interactively like this:

$ at 11:20 AM
warning: commands will be executed using /bin/sh
at> echo "hello world" > ~/at-test.txt
at> <EOT>
job 3 at Mon Jul 26 11:20:00 2021

As shown above, the at command uses an intuitive and natural time format. You don't need to know the 24-hour clock or convert the time to UTC format. Usually, it can be understood by directly using the words commonly used in our lives, such as noon , 1:30 PM , 13:37 etc.

After 11:20, you can use the cat command to check whether the at-test.txt file has content written:

$ cat ~/at-test.txt
hello world

Use the at command to schedule tasks

If you don't want to use the interactive way to at command, you can use the pipeline to pass the value of echo or printf at command.

For example, I want to write the hello again statement at-test.txt

$ echo "echo 'hello again' >> ~/at-test.txt" | at now +1 minute

After one minute, verify that the new command has been executed:

$ cat ~/at-test.txt
hello world
hello again

Time expression

As mentioned earlier, the at command uses the time format used in our daily lives, which is very convenient:

  • YYMMDDhhmm[.ss]
    (Abbreviated year, month, day, hour, minute [second])
  • CCYYMMDDhhmm[.ss]
    (Full year, month, day, hour, minute, and [second])
  • now
  • midnight
  • noon
  • teatime` (4 pm)
  • AM
  • PM

The time and date can be absolute, you can also add a plus sign ( + ) to make them relative to now . When specifying relative time, the following vocabulary used in daily life can be used:

  • minutes
  • hours
  • days
  • weeks
  • months
  • years

The following are some at valid expressions for the 061834cbbcdfce command:

$ echo "rsync -av /home/tux me@myserver:/home/tux/" | at 3:30 AM tomorrow
$ echo "/opt/batch.sh ~/Pictures" | at 3:30 AM 08/01/2022
$ echo "echo hello" | at now + 3 days

View at task queue

As time goes by, we can forget at command queue. If you want to check, you can use the atq command:

$ atq
10 Thu Jul 29 12:19:00 2021 a tux
9  Tue Jul 27 03:30:00 2021 a tux
7  Tue Jul 27 00:00:00 2021 a tux

To delete a task from the queue, use the atrm command and the task number. For example, to delete task 7:

$ atrm 7
$ atq
10 Thu Jul 29 12:19:00 2021 a tux
9  Tue Jul 27 03:30:00 2021 a tux

If you want to view the specific content of the scheduled task, you must view at spool . Only the root user can view the at spool, so you must use sudo to view the content of the spool or cat

summary

at command is a very good way to avoid forgetting the task at a specific time in the future or let the computer run the task for you when you are away. Unlike the cron command, it does not need to make the task run in a loop from now on, so its syntax is cron simpler than that of 061834cbbd07c2.

Next time you want to let the system do a job for you after work, you can try the at command.


Finally, recently, many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will be helpful to you!

Give it to everyone for free, just ask you to give me a thumbs up!

e-book | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope that the old guys will have a three-strike combo, so that more people can read this article

Recommended reading:


良许
1k 声望1.8k 粉丝