at (排一个或多个命令在将来的指定时间执行)

rose1 发表于 2020-08-11 11:34
浏览次数:
在手机上阅读

在类似Unix的操作系统上,at,batch,atq和atrm命令可以安排一个或多个命令在将来的指定时间执行。 本文档介绍了at,batch,atq和atrm的GNU / Linux版本。

查看英文版

目录

1 at 运行系统环境

2 at

3 at 语法

4 at 例子

at 运行系统环境

Linux

at

at命令将命令安排在通常具有运行许可权的特定时间运行一次。at命令可以是任何内容,从简单的提醒消息到复杂的脚本。首先,在命令行上运行at命令,并将计划时间作为选项传递给它。然后,它会在特殊提示下显示您,您可以在其中键入要在计划的时间运行的命令(或一系列命令)。完成后,在新行上按Control-D,您的命令将被放入队列中。

典型的at命令序列如下所示(您输入的命令在此处以蓝色框显示,或在下面以黑体显示):

at 9:30 PM Tue
warning: commands will be executed using /bin/sh
at> echo "It's 9:30 PM on Sunday." at> ^D job 1 at Sun Nov 16 09:30:00 2014

当我们运行的命令后,第一件事,在做的是给我们一个“警告”,告诉我们什么命令shell我们的命令将运行:在这种情况下,/ bin / sh的,在Bourne Shell中。该外壳是传统的标准Unix外壳。

然后将其置于at>提示符下。在这里,我们键入一个简单的echo命令,该命令将回显文本字符串。我们按Enter键,然后将其放置在新的>提示符下。然后,我们按控制-d ,告诉在我们都与我们的命令来完成。然后告诉我们,我们的工作是工作编号1,它将在下周二运行。

注意

您指定命令的输出将被邮寄给您。您可以使用邮件程序或诸如pine(或pine的现代版本,称为alpine)之类的程序阅读此邮件。如果没有这些程序,则可以下载它们,也可以通过软件包管理器进行安装。例如,在使用APT软件包管理系统的Ubuntu上,可以使用apt-get命令安装它们,特别是:sudo apt-get install mailsudo apt-get install alpine。

指定时间

at使用时间和日期的非常随意的表示。它甚至知道一些您可能不会想到的“常用时间”,例如,“下午茶时间”传统上是下午4点。

这是您可以传递给at安排命令时间的一些示例。例如,假设当前时间是2014年10月18日(星期二)上午10:00。以下表达式将转换为以下时间:

表达方式: 将转换为:
中午

2014年10月18日中午12:00

午夜

2014年10月19日中午12:00

下午茶时间

2014年10月18日下午4:00

明天

2014年10月19日,上午10:00

明天中午    

2014年10月19日中午12:00

下周

2014年10月25日上午10:00

下个星期一

2014年10月24日上午10:00

星期五 

2014年10月21日,上午10:00

十一月

2014年11月18日上午10:00    

9:00 AM

2014年10月19日上午9:00

2:30 PM

2014年10月18日下午2:30

1430

2014年10月18日下午2:30

明天下午2:30

2014年10月19日下午2:30

下个月下午2:30

2014年11月18日下午2:30

星期五2:30 PM

2014年10月21日下午2:30

10/21下午2:30

2014年10月21日下午2:30    

10月21日下午2:30 

2014年10月21日下午2:30

2014/10/21下午2:30

2014年10月21日下午2:30

14年10月21日下午2:30 

2014年10月21日下午2:30

现在+ 30分钟

2014年10月18日上午10:30

现在+ 1小时

2014年10月18日上午11:00 

现在+ 2天

2014年10月20日上午10:00

下午4点+ 2天

2014年10月20日下午4:00

现在+ 3周

2014年11月8日上午10:00

现在+ 4个月

2015年2月18日上午10:00

现在+ 5年

2019年10月18日10:00 AM

...因此,如果您运行命令:

at now + 10 years

...然后在at>提示符下输入命令,按Enter,然后键入Control-D,您将在十年后收到命令结果的邮件。

注意

如果您未在命令行中指定时间,则at将返回以下错误消息:

Garbled time

...并且没有作业将被添加到队列中。因此,请始终在命令行中指定您的时间。

使用atq查看您的at队列

您可以使用程序atq来查看当前排队的工作。键入atq以显示队列。 

atq
1      Fri Oct 22 09:48:00 2014 a hope

该信息从左到右分别是:作业编号,日期,小时,年份,队列和用户名。

atq将仅列出属于您的作业-除非您是超级用户,否则它将列出所有用户的作业。因此,要列出所有在当前排队系统上的作业,键入此命令(如果你有超级用户权限):

sudo atq

...并在出现提示时输入密码。

The at command schedules a command to be run once at a particular time that you normally have permission to run. The at command can be anything from a simple reminder message, to a complex script. You start by running the at command at the command line, passing it the scheduled time as the option. It then places you at a special prompt, where you can type in the command (or series of commands) to be run at the scheduled time. When you're done, press Control-D on a new line, and your command will be placed in the queue.

A typical at command sequence looks like this (commands you type are shown here in the blue box, or in bold face below):

at 9:30 PM Tue
warning: commands will be executed using /bin/sh
at> echo "It's 9:30 PM on Sunday." at> ^D job 1 at Sun Nov 16 09:30:00 2014

When we ran the command, the first thing at did was give us a "warning" telling us what command shell our commands will be run with: in this case, /bin/sh, the Bourne Shell. This shell is the traditional standard Unix shell.

It then places us at the at> prompt. Here we type in a simple echo command, which echoes a string of text. We press enter, and we're placed at a new at> prompt. We then press Control-D, telling at we're all done with our commands. It then tells us that our job is job number 1 and that it will run next Tuesday.

Note

The output of your specified command will be mailed to you. You can read this mail with the mail program, or a program like pine (or the modern version of pine, called alpine). You can download these programs if you don't have them, or install them with your package manager; for example, on Ubuntu, which uses the APT package management system, you can install them using the apt-get command, specifically: sudo apt-get install mail or sudo apt-get install alpine.

Specifying Time

at uses a very casual representation of time and date. It even knows some "commonly used" times you might not expect — it knows that "teatime" is traditionally at 4 PM, for instance.

Here are some examples of times you can pass to at to schedule a command. For instance, let's assume the current time is 10:00 AM, Tuesday, October 18, 2014. The following expressions would translate to the following times:

the expression: would translate to:
noon

12:00 PM October 18 2014

midnight

12:00 AM October 19 2014

teatime

4:00 PM October 18 2014

tomorrow

10:00 AM October 19 2014

noon tomorrow

12:00 PM October 19 2014

next week

10:00 AM October 25 2014

next monday

10:00 AM October 24 2014

fri

10:00 AM October 21 2014

NOV

10:00 AM November 18 2014

9:00 AM

9:00 AM October 19 2014

2:30 PM

2:30 PM October 18 2014

1430

2:30 PM October 18 2014

2:30 PM tomorrow

2:30 PM October 19 2014

2:30 PM next month

2:30 PM November 18 2014

2:30 PM Fri

2:30 PM October 21 2014

2:30 PM 10/21

2:30 PM October 21 2014

2:30 PM Oct 21

2:30 PM October 21 2014

2:30 PM 10/21/2014

2:30 PM October 21 2014

2:30 PM 21.10.14

2:30 PM October 21 2014

now + 30 minutes

10:30 AM October 18 2014

now + 1 hour

11:00 AM October 18 2014

now + 2 days

10:00 AM October 20 2014

4 PM + 2 days

4:00 PM October 20 2014

now + 3 weeks

10:00 AM November 8 2014

now + 4 months

10:00 AM February 18 2015

now + 5 years

10:00 AM October 18 2019

...so if you run the command:

at now + 10 years

...and then enter a command at the at> prompt, press enter, and type Control-D, you will be mailed the results of your command ten years from now.

Note

If you don't specify a time at the command line, at will return the following error message:

Garbled time

...and no job will be added to the queue. So, always specify your time at the command line.

Using atq To View Your at Queue

You can use the program atq to view your currently-queued at jobs. Type atq to display the queue.

atq
1      Fri Oct 22 09:48:00 2014 a hope

This information is, from left to right: job numberdatehouryearqueue, and username.

atq will only list jobs that belong to you — unless you are the super user, in which case it will list the jobs of all users. So to list all at jobs currently queued on the system, type this command (if you have superuser privileges):

sudo atq

...and type your password, when prompted.

查看英文版

查看中文版

at 语法

at [-V] [-q queue] [-f file] [-mMlv] timespec...
at [-V] [-q queue] [-f file] [-mMkv] [-t time]
at -c job [job...]
atq [-V] [-q queue]
at [-rd] job [job...]
atrm [-V] job [job...]
batch
at -b

技术说明

at和从标准输入或指定文件中批量读取命令,这些命令将在以后使用sh执行。

at在指定时间执行命令。

atq列出用户的待处理作业,除非用户是超级用户;在这种情况下,列出了每个人的工作。输出行的格式(每个作业一个)是:作业号,日期,小时,年,队列和用户名。

atrm删除由作业编号标识的作业。

当系统负载级别允许时,批处理执行命令;换句话说,当平均负载下降到1.5以下或atd调用中指定的值以下时。

At允许相当复杂的时间规范,从而扩展了POSIX.2标准。它接受格式为HH:MM的时间以在一天的特定时间运行作业。(如果该时间已经过去,则假定为第二天。)您还可以指定午夜,中午下午茶时间(下午4点),并且可以在一天的后缀AM或PM进行早上或晚间。你还可以说什么一天的工作将给予形式月份名日的日期具有可选的一年,或给予形式的日期运行,MMDD [CC] YY,MM / DD / [CC] YY,DD.MM. [CC] YY或[CC] YY-MM-DD。日期的指定必须遵循一天中时间的指定。您还可以给出类似现在的 时间+计算时间单位,其中时间单位可以是分钟,小时,天或周,并且您可以告诉我们今天要加班,明天要加班,明天就来班在明天加上时间。

例如,要从现在开始三天的下午4点运行作业,您将在下午4点+ 3天运行,要在7月31日的上午10:00运行作业,您要在7月31日的上午10点运行作业,明天的凌晨1点运行作业。 ,你明天凌晨1点做。 

时间规范的定义可以在/ usr / share / doc / at / timespec中找到。

对于atbatch,命令都是从标准输入或使用-f选项指定的文件中读取并执行的。在工作目录中,环境(除变量BASH_VERSINFO,DISPLAY,EUID,组,SHELLOPTS,TERM,UID,和_)和umask的是从调用时保留。

由于当前被实现为setuid程序,因此也不会导出其他环境变量(例如LD_LIBRARY_PATH或LD_PRELOAD)。将来可能会改变。解决方法是,在作业中显式设置这些变量。

从su shell运行的atbatch命令将保留当前的用户ID。将向用户发送标准错误和命令中的标准输出(如果有)。邮件将使用命令/ usr / sbin / sendmail发送。如果从su shell 执行at ,则登录shell 的所有者将收到邮件。

超级用户可以始终使用这些命令。对于其他用户,使用at的权限由文件/etc/at.allow和/etc/at.deny确定。有关详细信息,请参见at.allow

选件

-V

将版本号打印为标准错误并成功退出。

-q queue

使用指定的队列。队列名称由一个字母组成;有效的队列名称范围从a到z,从AZa队列是at的默认队列,b队列是批处理的队列。具有较高字母的队列以增加的优美度运行。特殊队列“ =”保留给当前正在运行的作业。

如果将作业提交到以大写字母指定的队列中,则将作业视为在作业时已批量提交。一旦达到该时间,就将应用有关平均负载的批处理规则。如果给atq一个特定的队列,它将仅显示该队列中的未决作业。

-m

即使没有输出,作业完成后也要向用户发送邮件。

-M

切勿向用户发送邮件。换句话说,执行命令,但不要将其输出通知用户。

-f file

从文件而不是标准输入中读取作业。

-t time

以[[CC] YY] MMDDhhmm [.ss]格式给出的时间运行作业。

-l

-l上运行与在atq上运行相同;它显示所有排队的作业。

-r

与运行atrm相同。它从at队列中删除作业。

-d

也是atrm的别名。

-b

是批处理的别名。

-v

显示在读取作业之前作业将被执行的时间。显示的时间将采用“ Thu Feb 20 14:50:00 1997”的格式。

-c

将命令行中列出的作业分类为标准输出。

档案

/var/spool/cron/atjobs
/var/spool/cron/atspool
/proc/loadavg
/var/run/utmp
/etc/at.allow
/etc/at.deny

at [-V] [-q queue] [-f file] [-mMlv] timespec...
at [-V] [-q queue] [-f file] [-mMkv] [-t time]
at -c job [job...]
atq [-V] [-q queue]
at [-rd] job [job...]
atrm [-V] job [job...]
batch
at -b

Technical Description

at and batch read commands from standard input or a specified file that are to be executed at a later time, using sh.

at executes commands at a specified time.

atq lists the user's pending jobs, unless the user is the superuser; in that case, everybody's jobs are listed. The format of the output lines (one for each job) is: job numberdatehouryearqueue, and username.

atrm deletes jobs, identified by their job number.

batch executes commands when system load levels permit; in other words, when the load average drops below 1.5, or the value specified in the invocation of atd.

At allows fairly complex time specifications, extending the POSIX.2 standard. It accepts times of the form HH:MM to run a job at a specific time of day. (If that time is already past, the next day is assumed.) You may also specify midnightnoon, or teatime (4pm) and you can have a time-of-day suffixed with AM or PM for running in the morning or the evening. You can also say what day the job will be run, by giving a date in the form month-name day with an optional year, or giving a date of the form MMDD[CC]YYMM/DD/[CC]YYDD.MM.[CC]YY or [CC]YY-MM-DD. The specification of a date must follow the specification of the time of day. You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks and you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.

For example, to run a job at 4pm three days from now, you would do at 4pm + 3 days, to run a job at 10:00am on July 31, you would do at 10am Jul 31 and to run a job at 1am tomorrow, you would do at 1am tomorrow.

The definition of the time specification can be found in /usr/share/doc/at/timespec.

For both at and batch, commands are read from standard input or the file specified with the -f option and executed. The working directory, the environment (except for the variables BASH_VERSINFODISPLAYEUIDGROUPSSHELLOPTSTERMUID, and _) and the umask are retained from the time of invocation.

As at is currently implemented as a setuid program, other environment variables (e.g., LD_LIBRARY_PATH or LD_PRELOAD) are also not exported. This may change in the future. As a workaround, set these variables explicitly in your job.

An at or batch command run from a su shell will retain the current userid. The user will be mailed standard error and standard output from his commands, if any. Mail will be sent using the command /usr/sbin/sendmail. If at is executed from a su shell, the owner of the login shell will receive the mail.

The superuser may always use these commands. For other users, permission to use at is determined by the files /etc/at.allow and /etc/at.deny. See at.allow for details.

Options

-V

Prints the version number to standard error and exits successfully.

-q queue

Uses 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 that 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.

-m

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

-M

Never send mail to the user. In other words, execute the command, but do not notify the user of its output.

-f file

Reads the job from file rather than standard input.

-t time

Run the job at time, given in the format [[CC]YY]MMDDhhmm[.ss].

-l

Running at -l is the same as running atq; it displays all queued at jobs.

-r

Is the same as running atrm. It removes a job from the at queue.

-d

Is also an alias for atrm.

-b

Is an alias for batch.

-v

Shows 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".

-c

The cats the jobs listed on the command line to standard output.

Files

/var/spool/cron/atjobs
/var/spool/cron/atspool
/proc/loadavg
/var/run/utmp
/etc/at.allow
/etc/at.deny

查看英文版

查看中文版

at 例子

at -m 01:35 < my-at-jobs.txt

1:35 AM 运行“ my-at-jobs.txt ”文件中列出的命令。作业的所有输出将通过邮件发送给运行任务的用户。成功输入此命令后,您将收到类似于以下示例的提示:

commands will be executed using /bin/sh
job 1 at Wed Dec 24 00:22:00 2014
at -l

此命令将以如下格式列出每个计划的作业: 

1          Wed Dec 24 00:22:00 2003

...这与运行命令atq相同。

at -r 1

删除作业1。该命令与运行命令atrm 1相同。

atrm 23

删除作业23。此命令与at-r 23处运行命令相同。

at -m 01:35 < my-at-jobs.txt

Run the commands listed in the 'my-at-jobs.txt' file at 1:35 AM. All output from the job will be mailed to the user running the task. When this command has been successfully entered you should receive a prompt similar to the example below:

commands will be executed using /bin/sh
job 1 at Wed Dec 24 00:22:00 2014
at -l

This command will list each of the scheduled jobs in a format like the following:

1          Wed Dec 24 00:22:00 2003

...this is the same as running the command atq.

at -r 1

Deletes job 1. This command is the same as running the command atrm 1.

atrm 23

Deletes job 23. This command is the same as running the command at -r 23.

查看英文版

查看中文版

其他命令行

apt-cache | apt-get | ar | arch | arp | as | a2p | aspell | ac | awk | adduser | a2p | apropos | alias | agrep | addgroup |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条