Linux at命令:在一个指定的时间执行一个指定任务

Linux at命令:安排命令在特定时间运行一次,检查或删除作业:at、batch、atq、atrm

Syntax
      at [-V] [-q queue] [-f file] [-mldbv] TIME          # Execute commands at a specified time. 
      at [-V] [-q queue] [-f file] [-mldbv] -t time_arg   # Execute commands at a specified time. 
      at -c job [job...]
      atq [-V] [-q queue]       # List the user's pending jobs, or for the superuser; list all jobs.
      atrm [-V] job [job...]    # Delete jobs, identified by their job number.
      batch                     # Execute commands when system load levels permit.

Key
   -c    Cat the jobs listed on the command line to standard output.

   -d    Is an alias for atrm.

   -f file
         Read the job from file rather than standard input.

   -l    Is an alias for atq.

   -m    Send mail to the user when the job has completed even if there was no output.

   -q queue
         use the specified queue.
         A queue designation consists of a single letter; valid queue designations range from a to z.
         and A to Z. The a queue is the default for at and the b queue for batch.
         Queues with higher letters run with increased niceness.
         The special queue "=" is reserved for jobs which are currently running. If a job is submitted to a queue
         designated with an uppercase letter, the job is treated as if it were submitted to batch at the time of
         the job. Once the time is reached, the batch processing rules with respect to load average apply.
         If atq is given a specific queue, it will only show jobs pending in that queue.

   -t time_arg
         Submit the job to be run at the time specified by the time_arg option argument, which must have
         the same format as specified for the touch utility's -t time option argument ([[CC]YY]MMDDhhmm). 

   -v    Show the time the job will be executed before reading the job. 
         Times displayed will be in the format "Thu Feb 20 14:50:00 1997".

   -V    Print the version number to standard error.

atbatch读取标准输入或一个指定文件,它们将会在稍后被执行。

在系统负载水平允许时批量执行命令;换句话说,当负载平均值低于 0.8 或 atd 调用中指定的值时。

at允许复杂的时间规范,扩展了 POSIX.2 标准。它接受HH: MM形式的时间以在一天中的特定时间运行作业。(如果该时间已经过去,则假定为第二天。)你还可以指定午夜、中午或下午茶时间(下午 4 点),并且你可以指定一个以 AM 或 PM 为后缀的时间,以在早上或下午运行晚上。你还可以通过以月份名称日期形式提供日期和可选年份,或以MMDDYY或MM / DD / YY或DD.MM.YY或YYYY形式提供日期来说明作业将在哪一天运行-MM-DD. 日期的指定必须遵循时间的指定。你还可以给出像now + count 时间单位这样的时间,其中时间单位可以是分钟、小时、天或周,你可以通过在今天加上时间后缀来告诉 at 今天运行作业,明天运行作业用明天作为时间的后缀。

例如,从现在起三天后的下午 4 点运行作业:下午 4 点 + 3 天,  7 月 31 日上午 10:00 运行作业:7 月 31 日上午 10 点  运行作业,明天凌晨 1 点运行作业:明天凌晨 1 点。 时间规范的确切定义可以在 /usr/share/doc/at-3.1.10/timespec 中找到。

对于 at 和 batch,从标准输入或使用 -f 选项指定的文件读取命令并执行。工作目录、环境(变量 TERM、DISPLAY 和 _ 除外)和 umask 从调用时起保留。从su调用的at或 batch 命令将保留当前用户 ID。如果有的话,用户将收到命令的标准错误和标准输出。邮件将使用命令 /usr/sbin/sendmail 发送。如果从su执行 at,登录 shell 的所有者将收到邮件。

安装

大多数发行版默认包含 at,如果你需要安装它,请使用包管理器,例如apt。

> sudo apt update  
> sudo apt install at   
> sudo apt-get install at

权限

超级用户在任何情况下都可以使用这些命令。对于其他用户,使用 at 的权限由文件 /etc/at.allow 和 /etc/at.deny 确定。

  • 如果文件 /etc/at.allow 存在,则只允许其中提到的用户名使用 at。
  • 如果 /etc/at.allow 不存在,则检查 /etc/at.deny,然后允许其中未提及的每个用户名使用 at。
  • 如果两者都不存在,则只允许超级用户使用 at。

空的 /etc/at.deny 表示允许每个用户使用这些命令,这是默认配置。

环境

at 调用时 SHELL 环境变量的值将决定使用哪个 shell 来执行 at 作业命令。如果调用at 时未设置 SHELL,则将使用用户的登录 shell;否则,如果在调用 at 时设置了 SHELL,则它必须包含 shell 解释器可执行文件的路径,该可执行文件将用于在指定时间运行命令。

at 将记录调用时存在的环境变量的值。当命令在指定时间运行时,at 会将这些变量恢复到它们记录的值。这些变量被排除在此处理之外,并且在运行命令时永远不会由 at 设置:TERM、DISPLAY、SHELLOPTS、PPID、BASH_VERSINFO、EUID、UID、GROUPS。

例子

在晚上 11 点运行export1.sh脚本:

> echo "/home/scripts/export1.sh" | 23:00

在上面的示例中,我们将所需的命令传递给at,没有at将提示带有at>提示,你可以在其中交互式输入所需的命令,完成后按 Ctrl-D 退出。

> at 23:00 <<Linux  
"/home/scripts/export1.sh"  
Linux

要运行的作业也可以存储在文件中并使用 -f 执行,如果文件包含多行/命令,则每个命令作为单独的作业启动:

> echo "/home/scripts/export1.sh" >/home/scripts/myjob.sh
>  at 23:00 -f /home/scripts/myjob.sh

两天后的下午 6 点执行 /bin/ls

> at 6pm+2 days
at> /bin/ls
at> <EOT>
job 7 at 2022-09-28 18:00

查看at任务

>  atq
7       Wed Sep 28 18:00:00 2022 a root

显示已经设置的任务内容

> at -c 7
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
...
${SHELL:-/bin/sh} << 'marcinDELIMITER0ae52d8c'
/bin/ls

marcinDELIMITER0ae52d8c

删除已经设置的任务

> atrm 7
© 版权声明
THE END
喜欢就支持一下吧
点赞131赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称代码图片

    暂无评论内容