Linux 从现在起 5 秒后运行命令 `at `

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13905886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:03:31  来源:igfitidea点击:

Run command `at ` 5 seconds from now

linuxbashscheduled-tasks

提问by inspectorG4dget

As part of a slightly complex script, I need to tell a server to run a simulation. Normally, I would achieve this by doing ssh user@server 'simulation/script'. However, doing so would keep the ssh session alive until 'simulation/script'is done, which is undesirable to me.

作为稍微复杂的脚本的一部分,我需要告诉服务器运行模拟。通常,我会通过执行ssh user@server 'simulation/script'. 但是,这样做会使 ssh 会话保持活动状态直到'simulation/script'完成,这对我来说是不可取的。

I recently learned about the atcommand, and it seems to fit into my problem well.
What I want to do now is to ssh into my server, and atmy simulation script to run in 5 seconds (more than enough time for the ssh connection to be closed). Thus, once the ssh connection is closed within 5 seconds, the server will start the simulation without needing the ssh connection to stay alive.

我最近了解了该at命令,它似乎很适合我的问题。
我现在想要做的是通过 ssh 进入我的服务器,并且at我的模拟脚本在 5 秒内运行(足够关闭 ssh 连接的时间)。因此,一旦 ssh 连接在 5 秒内关闭,服务器将开始模拟,而无需 ssh 连接保持活动状态。

What I'm having trouble with is the time expression that atneeds in order to schedule a job "5 seconds from now"

我遇到的问题是at“从现在起 5 秒”安排工作所需的时间表达式

I have tried the following time expressions, all of which give me errors:

我尝试了以下时间表达式,所有这些都给我错误:

now + 5 seconds
now + 5 sec
now + 5 s
now + 5seconds
now + 5sec
now + 5 s
now+5sec
now+5seconds
now+5s

How can I get my atto run my command "5 seconds from now"?

我怎样才能让我at“从现在起 5 秒”运行我的命令?

采纳答案by Gilles Quenot

There's no secondsin at :

没有

man atsaid :

man at说过 :

  • 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.
  • 日期的规范必须遵循一天中的时间规范。您还可以提供像 now + count time-units 这样的时间,其中时间单位可以是分钟、小时、天或周,您可以通过在时间后加上今天来告诉 at 今天运行作业并明天运行作业通过在时间后缀明天。

So instead of at, you could use a sleepI think.

因此at,您可以使用sleepI think代替。

See man 1 sleep

man 1 sleep



If you'd like to run ssh user@server 'simulation/script'without waiting, simply do :

如果您想ssh user@server 'simulation/script'不等待就运行,只需执行以下操作:

ssh user@server 'simulation/script' &

the command will run in the background.

该命令将在后台运行。

Moreover, as Rawkode said, nohupwill help there.

此外,正如 Rawkode 所说,nohup将在那里提供帮助。

So finally :

所以最后:

nohup ssh user@server 'simulation/script' &

with nohup, you can quit your terminal and have the sshprocess alive.

使用 nohup,您可以退出终端并使ssh进程处于活动状态。



EDIT: if you want to run the sshcommand and close the connection :

EDIT:如果要运行ssh命令并关闭连接:

ssh user@server 'simulation/script &'

回答by Rawkode

atdoesn't use seconds, only minutes/hours/days

at不使用秒,只使用分钟/小时/天

What you can do is precede your script with nohup, which will ensure the script isn't killed when you disconnect your SSH session.

您可以做的是在您的脚本之前加上nohup,这将确保在您断开 SSH 会话时脚本不会被终止。

ssh server 'nohup yourscript.sh &'

ssh server 'nohup yourscript.sh &'

NOTE:Having just played with the above, the SSH connection has to be killed manually.

注意:刚刚玩过上面的,SSH 连接必须手动终止。

Another alternative would be screen

另一种选择是 screen

screen -d -m yourscript.sh

screen -d -m yourscript.sh

This will launch a detached screenprocess that you can reattach to at any time later.

这将启动一个分离的screen进程,您可以在以后随时重新附加到该进程。

NOTE:I've tested this with the following script and command and it worked perfectly.

注意:我已经使用以下脚本和命令对此进行了测试,并且效果很好。

SSH command

SSH 命令

ssh server.com 'screen -d -m ~/myscript.sh'

ssh server.com 'screen -d -m ~/myscript.sh'

myscript.sh

脚本文件

#!/bin/sh
sleep 10
echo "hello world" > /tmp/hello
exit;

回答by Preston

Redirecting stdin/stdout/stderr in addition to backgrounding the script will allow the SSH session to close immediately after executing the backgrounded command:

除了后台脚本之外,重定向 stdin/stdout/stderr 将允许 SSH 会话在执行后台命令后立即关闭:

ssh hostname "/path/to/script </dev/null >/dev/null 2>/dev/null &"

Source: https://serverfault.com/a/36436/138334.

来源:https: //serverfault.com/a/36436/138334

回答by sdaau

Just to note: in man at, I saw there is a -tswitch, which will accept date times with seconds - but unfortunately the seconds will be truncated:

请注意:在man at,我看到有一个-t开关,它将接受带秒的日期时间 - 但不幸的是秒数将被截断:

$ date; date --date="now +10 seconds" +"%m%d%H%M.%S"; echo "logger AAAA" | at -t $(date --date="now +5 seconds" +"%Y%m%d%H%M.%S")
Thu Feb  5 14:45:57 CET 2015
02051446.07
warning: commands will be executed using /bin/sh
job 8 at Thu Feb  5 14:46:00 2015

... and so the job may actually be scheduled in the past (also, used loggerto syslog, because it doesn't look like echoing to terminals' stdout can work from here)

...因此该作业实际上可能是在过去安排的(也用于logger系统日志,因为它看起来不像回显到终端的标准输出可以从这里工作)

回答by user6787662

I think it is much easier doing:

我认为这样做要容易得多:

sleep n && command   

where nis number of seconds.

哪里n是秒数。

回答by TomOnTime

"at" doesn't have sub-minute resolution but you can fake it:

“at”没有亚分钟分辨率,但你可以伪造它:

echo "sleep 5 ; COMMAND" | at now

回答by ijj

You can do it using sleep command like that:

你可以使用 sleep 命令来做到这一点:

bash -c 'sleep 5 ; echo "test"' &

回答by Xaero Degreaz

I ran into the same issue today, but I was able to resolve it using nohup

我今天遇到了同样的问题,但我能够使用 nohup

nohup bash -c 'sleep 5 && at now -f script-name.sh'

nohup bash -c 'sleep 5 && at now -f script-name.sh'