mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translated
This commit is contained in:
parent
e79359d74f
commit
7c72b0f171
@ -1,168 +0,0 @@
|
||||
[#]: subject: "Schedule a task with the Linux at command"
|
||||
[#]: via: "https://opensource.com/article/21/8/linux-at-command"
|
||||
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Schedule a task with the Linux at command
|
||||
======
|
||||
The at command is the Linux terminal's method of allowing you to
|
||||
schedule one-time jobs for a specific time and date.
|
||||
![Team checklist][1]
|
||||
|
||||
Computers are good at [automation][2], but not everyone knows how to make automation work. It's a real luxury, though, to be able to schedule a task for a computer at a specific time and then forget about it. Maybe you have a file to upload or download at a specific time, or you need to process a batch of files that don't yet exist but are guaranteed to exist by a certain time or a setting that needs monitoring, or maybe you just need a friendly reminder to pick up bread and butter on the way home from work.
|
||||
|
||||
That's what the `at` command is for.
|
||||
|
||||
### What is the Linux at command?
|
||||
|
||||
The `at` command is the Linux terminal's method of allowing you to schedule one-time jobs for a specific time and date. It's spontaneous automation made easy from the terminal.
|
||||
|
||||
### Installing at
|
||||
|
||||
On Linux, the `at` command is probably already installed. You can verify that it's installed using the `at -V` command. As long as a version is returned, you have `at` installed:
|
||||
|
||||
|
||||
```
|
||||
$ at -V
|
||||
at version x.y.z
|
||||
```
|
||||
|
||||
Should you try to use `at` and the command isn't found, most modern Linux distributions offer to install the missing `at` package for you.
|
||||
|
||||
### Scheduling a job interactively with at
|
||||
|
||||
When you use the `at` command along with the time you want a task to run, you open an interactive `at` prompt. You can enter commands you want to run at the time you specified.
|
||||
|
||||
If it helps, you can think of this process as a calendaring app, like the one you probably use on your mobile phone—first, you create an event on a day at some time, and then you specify what you want to happen.
|
||||
|
||||
For example, try scheduling a memo to yourself by creating a task for a few minutes in the future. Make the task simple to reduce the likelihood of failure. To exit the `at` prompt, press **Ctrl+D** on your keyboard.
|
||||
|
||||
|
||||
```
|
||||
$ 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 you can see, `at` uses intuitive and natural time definitions. You don't need to know the 24-hour clock or translate times to UTC or a specific ISO format. Generally, you can use whatever notation comes naturally to you, such as _noon_, _1:30 PM_, _13:37_, and so on, to describe when you want a task to happen.
|
||||
|
||||
Wait a few minutes and then verify that your task has run by running the `cat` or the `tac` command on the file you created:
|
||||
|
||||
|
||||
```
|
||||
$ cat ~/at-test.txt
|
||||
hello world
|
||||
```
|
||||
|
||||
### Scheduling a job with at
|
||||
|
||||
You don't have to use the interactive prompt to schedule jobs with `at`. You can instead pipe commands to it using `echo` or `printf`. In this example, I use the notation _now_ and how many minutes from now I want the task to be delayed:
|
||||
|
||||
|
||||
```
|
||||
`$ echo "echo 'hello again' >> ~/at-test.txt" | at now +1 minute`
|
||||
```
|
||||
|
||||
After a minute, verify that the new command has been executed:
|
||||
|
||||
|
||||
```
|
||||
$ cat ~/at-test.txt
|
||||
hello world
|
||||
hello again
|
||||
```
|
||||
|
||||
### Time expressions
|
||||
|
||||
The `at` command is pretty forgiving when interpreting times. You can choose between many formats, depending on which is most convenient for you:
|
||||
|
||||
* `YYMMDDhhmm`[.ss]
|
||||
(abbreviated year, month, day, hour, minute, and optionally seconds)
|
||||
* `CCYYMMDDhhmm`[.ss]
|
||||
(full year, month, day, hour, minute, and optionally seconds)
|
||||
* `now`
|
||||
* `midnight`
|
||||
* `noon`
|
||||
* `teatime` (4 PM)
|
||||
* `AM`
|
||||
* `PM`
|
||||
|
||||
|
||||
|
||||
Times and dates can be absolute or add a plus sign (_+_) to make them relative to _now_. When specifying relative times, you can use words you probably already use:
|
||||
|
||||
* `minutes`
|
||||
* `hours`
|
||||
* `days`
|
||||
* `weeks`
|
||||
* `months`
|
||||
* `years`
|
||||
|
||||
|
||||
|
||||
### Time and date syntax
|
||||
|
||||
The `at` command is less forgiving in telling times from dates. The time must come first, followed by the day, although the day defaults to the current day and is only required when scheduling a task for someday in the future.
|
||||
|
||||
These are examples of just a few valid expressions:
|
||||
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
|
||||
### Viewing your at queue
|
||||
|
||||
Once you've embraced `at` and are scheduling tasks instead of scribbling notes to yourself on scraps of paper lying around your desk, you may want to review whether you've got jobs still in the queue.
|
||||
|
||||
To view your `at` queue, 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 remove a task from your queue, use the `atrm` command along with the job number. To remove job 7, for instance:
|
||||
|
||||
|
||||
```
|
||||
$ atrm 7
|
||||
$ atq
|
||||
10 Thu Jul 29 12:19:00 2021 a tux
|
||||
9 Tue Jul 27 03:30:00 2021 a tux
|
||||
```
|
||||
|
||||
To see what's actually in a scheduled job, you have to look at the `at` spool. Only the root user can view the `at` spool, so you must use `sudo` to view the spool or to `cat` the contents of any job.
|
||||
|
||||
### Schedule with Linux at
|
||||
|
||||
The `at` system is an excellent way to avoid forgetting to run a job later in the day or to have your computer run a job for you when you're away. Unlike `cron`, it's free from the expectation that a job must run on a regular schedule from now until perpetuity, and its syntax is consequently a lot simpler than the `cron` syntax.
|
||||
|
||||
Next time you have a small job you want your computer to remember and manage, try the `at` command.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/8/linux-at-command
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/checklist_todo_clock_time_team.png?itok=1z528Q0y (Team checklist)
|
||||
[2]: https://opensource.com/article/20/11/orchestration-vs-automation
|
@ -0,0 +1,169 @@
|
||||
[#]: subject: "Schedule a task with the Linux at command"
|
||||
[#]: via: "https://opensource.com/article/21/8/linux-at-command"
|
||||
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
用 Linux 的命令来安排一个任务
|
||||
======
|
||||
at 命令是一种在特定时间和日期安排一次性任务的终端方法。
|
||||
![Team checklist][1]
|
||||
|
||||
计算机擅长[自动化][2],但不是每个人都知道如何使自动化工作。不过,能够在特定的时间为电脑安排一个任务,然后忘记它,这确实是一种奢侈。也许你有一个文件要在特定的时间上传或下载,或者你需要处理一批还不存在但保证在某个时间存在的文件,或者需要监控的设置,或者你只是需要一个友好的提醒,在下班回家的路上拿起面包和黄油。
|
||||
|
||||
That's what the `at`command is for.
|
||||
这就是 `at` 命令的用处。
|
||||
|
||||
### 什么是 Linux at 命令?
|
||||
|
||||
`at` 命令是 Linux 终端允许你在特定时间和日期安排一次性工作的方法。它是一种自发的自动化,在终端上很容易实现。
|
||||
|
||||
### 安装 at
|
||||
|
||||
在 Linux 上,`at` 命令可能已经安装了。你可以使用 `at -V` 命令来验证它是否已经安装。只要返回一个版本,就说明你已经安装了 `at`。
|
||||
|
||||
|
||||
```
|
||||
$ at -V
|
||||
at version x.y.z
|
||||
```
|
||||
|
||||
如果你试图使用 `at`,但没有找到该命令,大多数现代的 Linux 发行版会提供缺少的 `at` 包。
|
||||
|
||||
### 用 at 交互式地安排一个作业
|
||||
|
||||
当你使用 `at` 命令和你希望任务运行的时间时,你会打开一个交互式 `at` 提示。你可以输入你想在你指定的时间运行的命令。
|
||||
|
||||
如果有帮助的话,你可以把这个过程看作是一个日历应用,就像你可能在你的手机上使用的那样。首先,你在某一天的某个时间创建一个事件,然后指定你想要发生什么。
|
||||
|
||||
例如,尝试通过创建一个未来几分钟的任务来计划给自己的备忘录。让任务变得简单,以减少失败的可能性。要退出 `at` 提示,请按键盘上的 **Ctrl+D**。
|
||||
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
|
||||
正如你所看到的,`at` 使用直观和自然的时间定义。你不需要知道 24 小时制的时钟,也不需要把时间翻译成 UTC 或特定的 ISO 格式。一般来说,你可以使用你自然想到的任何符号,如 _noon_、_1:30 PM_、_13:37_ 等等,来描述你希望一个任务发生的时间。
|
||||
|
||||
等待几分钟,然后在你创建的文件上运行 `cat` 或者 `tac` 命令,验证你的任务是否已经运行:
|
||||
|
||||
|
||||
```
|
||||
$ cat ~/at-test.txt
|
||||
hello world
|
||||
```
|
||||
|
||||
### 用 at 安排一个任务
|
||||
|
||||
你不必使用 `at` 交互式提示符来安排任务。你可以使用 `echo` 或 `printf` 向它传送命令。在这个例子中,我使用了 _now_ 符号,以及我希望任务从现在开始延迟多少分钟:
|
||||
|
||||
|
||||
```
|
||||
`$ echo "echo 'hello again' >> ~/at-test.txt" | at now +1 minute`
|
||||
```
|
||||
|
||||
一分钟后,验证新的命令是否已被执行:
|
||||
|
||||
|
||||
```
|
||||
$ cat ~/at-test.txt
|
||||
hello world
|
||||
hello again
|
||||
```
|
||||
|
||||
### 时间表达式
|
||||
|
||||
`at` 命令在解释时间时是非常宽容的。你可以在许多格式中选择,这取决于哪一种对你来说最方便:
|
||||
|
||||
* `YYMMDDhhmm`[.ss]
|
||||
(缩写的年、月、日、小时、分钟,也可选择秒)
|
||||
* `CCYYMMDDhhmm`[.ss]
|
||||
(完整的年、月、日、时、分,也可选择的秒)
|
||||
* `now`
|
||||
* `midnight`
|
||||
* `noon`
|
||||
* `teatime`(4 PM)
|
||||
* `AM`
|
||||
* `PM`
|
||||
|
||||
|
||||
|
||||
时间和日期可以是绝对的,也可以加一个加号(_+_),使其与 _now_ 相对。当指定相对时间时,你可以使用你可能已经使用的词语:
|
||||
|
||||
* `minutes`
|
||||
* `hours`
|
||||
* `days`
|
||||
* `weeks`
|
||||
* `months`
|
||||
* `years`
|
||||
|
||||
|
||||
|
||||
### 时间和日期语法
|
||||
|
||||
`at` 命令对日期的输入相比日期不那么宽容。时间必须放在第一位,接着是日期,尽管日期默认为当前日期,并且只有在为未来某天安排任务时才需要。
|
||||
|
||||
这些是一些有效表达式的例子:
|
||||
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
|
||||
### 查看你的 at 队列
|
||||
|
||||
当你接受了 `at`,并且正在安排任务,而不是在桌子上的废纸上乱写乱画,你可能想查看一下你是否有任务还在队列中。
|
||||
|
||||
|
||||
要查看你的 `at` 队列,使用 `atq` 命令:
|
||||
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
|
||||
要从队列中删除一个任务,使用 `atrm` 命令和任务号。例如,要删除任务 7:
|
||||
|
||||
|
||||
```
|
||||
$ atrm 7
|
||||
$ atq
|
||||
10 Thu Jul 29 12:19:00 2021 a tux
|
||||
9 Tue Jul 27 03:30:00 2021 a tux
|
||||
```
|
||||
|
||||
要看一个计划中的任务的实际内容,你需要查看 `at` spool。只有 root 用户可以查看 `at` spool,所以你必须使用 `sudo` 来查看 spool 或 `cat` 任何任务的内容。
|
||||
|
||||
### 用 Linux at 安排任务
|
||||
|
||||
`at` 系统是一个很好的避免忘记在一天中晚些时候运行一个作业,或者在你离开时让你的计算机为你运行一个作业的方法。与 `cron` 不同的是,它不像 `cron` 那样要求任务必须从现在起一直按计划运行到永远,因此它的语法比 `cron` 简单得多。
|
||||
|
||||
等下次你有一个希望你的计算机记住并管理它的小任务,试试 `at` 命令。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/8/linux-at-command
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/checklist_todo_clock_time_team.png?itok=1z528Q0y (Team checklist)
|
||||
[2]: https://opensource.com/article/20/11/orchestration-vs-automation
|
Loading…
Reference in New Issue
Block a user