atq1
Scheduling commands in IT environments are important. There are tools like at
, atq
, atrm
, batch
. We will look all of them in this tutorial.
在IT环境中安排命令很重要。 有一些工具,例如at
, atq
, atrm
, batch
。 我们将在本教程中查找所有这些内容。
安排工作时间 (Schedule Job At)
While scheduling jobs we will use at
command. At command have very causal time specification. We will look varying time specification in the next example in detail. Now we simply schedule job.
在计划作业时,我们将使用at
命令。 在命令中有非常因果的时间规范。 在下一个示例中,我们将详细介绍变化的时间规范。 现在我们只需安排工作。
$ at 20:58
We will run mkdir test
command at 20:58
. In order to accomplish this we run following steps.
我们将在20:58
运行mkdir test
命令。 为了做到这一点,我们运行以下步骤。
at 20:58
will run following commands at20:58
at 20:58
将在运行下面的命令20:58
mkdir test
is the command we want to run specified timemkdir test
是我们要在指定时间运行的命令We can give more than one command for each line of prompt but we can end the commands with
CTRL+d
which will give an output like<EOT>
我们可以为每一行提示提供多个命令,但可以使用
CTRL+d
结束命令,这将给出类似<EOT>
的输出
时间格式(Time Formats)
at
command supports a lot of different type of time specification. Here some of them.
at
命令支持许多不同类型的时间规范。 这里有一些。
noon
will run 12:00 PMnoon
将运行12:00 PMmidnight
will run 12:00 AMmidnight
将运行12:00 AMteatime
will run 4:00 PMteatime
将运行4:00 PMtomorrow
will run next day same timetomorrow
将同时运行next week
will run next weeknext week
将在下周运行fri
will run Fridayfri
将运行周五next monday
will run next mondaynext monday
将在下周一运行now + 1 hour
will run after 1 hournow + 1 hour
将在1小时后运行now + 3 weeks
will run after 3 weeksnow + 3 weeks
将在3周后运行
安排相对时间的工作(Schedule Job With Relative Time)
As we have seen a lot of different time expressions in previous example we can use them in our examples. In this example we will set time relatively which means we will set time according to current time. To specify relative time we will use now
time specifiers with count
. In the example we will set command to run after 15 minutes.
正如我们在前面的示例中看到的很多不同的时间表达式一样,我们可以在示例中使用它们。 在此示例中,我们将相对设置时间,这意味着我们将根据当前时间设置时间。 为了指定相对时间,我们将now
使用带有count
时间说明符。 在示例中,我们将命令设置为在15分钟后运行。
$ at now + 15 minutes
We can see from screenshot that the command is set to run at 21:31
which is after 15 minutes from now. We can see that this job is numbered 2
从屏幕截图中我们可以看到,该命令设置为从现在开始15分钟后的21:31
运行。 我们可以看到这份工作编号为2
查看所有计划作业 (View All The Schedules Jobs)
In a busy system there may be a lot of scheduled jobs. Listing them can be done with atq
command.
在繁忙的系统中,可能会有很多计划的作业。 列出它们可以使用atq
命令完成。
$ atq
We can see that there is only one job in the list. This job is numbered as 2
. This job will e run at Thu Mar 9 21:31:00
. The job is created by user ismail
我们可以看到列表中只有一项工作。 这项工作被编号为2
。 这项工作将在Thu Mar 9 21:31:00
。 该作业由用户ismail
创建
删除/删除预定作业(Remove/Delete A Scheduled Jobs)
We can also remove jobs that not run. We need the job ID which can be listed with atq . We will provide job ID to the atrm
command like below. We will remove job ID 2
.
我们还可以删除未运行的作业。 我们需要可以用atq列出的工作ID。 我们将为atrm
命令提供工作ID,如下所示。 我们将删除作业ID 2
。
$ atrm 2
根据平均负载执行作业(Execute Jobs According to Load Average)
We can set for different times for our jobs. But some times just setting time is not enough or not suitable for our situation. There is an alternative way to schedule command We can use batch
command to run commands if load average is less than 1.5
. In the example we will run echo "Run reports"
我们可以为工作设置不同的时间。 但是有时候仅仅设定时间是不够的,或者不适合我们的情况。 还有另一种调度命令的方法,如果平均负载小于1.5
则可以使用batch
命令来运行命令。 在示例中,我们将运行echo "Run reports"
$ batch
从文件重新加载作业(Reload Jobs From A File)
Another useful feature is reading commands from a file. As we know commands are read from an interactive shell. We can provide commands to be run at scheduled time. We will use -f
option and the command file name to use this feature. In the example we will read commands file named myjobs.txt
and run them after 2 days.
另一个有用的功能是从文件读取命令。 众所周知,命令是从交互式外壳读取的。 我们可以提供要在预定时间运行的命令。 我们将使用-f
选项和命令文件名来使用此功能。 在示例中,我们将读取名为myjobs.txt
命令文件,并在两天后运行它们。
$ at -f myjobs.txt now + 2 days
允许和拒绝用户计划作业(Allow and Deny User To Schedule Job)
By default all users in Linux system can create scheduled jobs. This may create some problems or security issues or simply it is unwanted situation. We can set rules to allow or deny some user from scheduling commands.
默认情况下,Linux系统中的所有用户都可以创建计划的作业。 这可能会引起一些问题或安全性问题,或者仅仅是不希望的情况。 我们可以设置规则以允许或拒绝某些用户调度命令。
There is two configuration file to configure this.
有两个配置文件可以对此进行配置。
at.allow
is used to set allowed usersat.allow
用于设置允许的用户at.deny
is used to set denied usersat.deny
用于设置拒绝的用户
翻译自: https://www.poftut.com/linux-scheduling-commands-atq-atrm-batch-examples/
atq1