bash 如何在一分钟后在linux中启动一个shell脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10497120/
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
How to start a shell script in one minute later in linux?
提问by Alex Chan
How to start a shell script in one minute later?
如何在一分钟后启动一个shell脚本?
Suppose there are two bash files a.sh and b.sh
I want to execute b.sh one minute(or several seconds) after a.sh executed.
what should I code in a.sh ?
假设有两个 bash 文件 a.sh 和 b.sh
我想在 a.sh 执行后一分钟(或几秒钟)执行 b.sh。
我应该在 a.sh 中编码什么?
回答by Mathieu J.
Simple. you want to use 'at' to schedule your job. and 'date' to calculate your moment in the future.
简单的。您想使用“at”来安排您的工作。和“日期”来计算你未来的时刻。
Example:
例子:
echo b.sh | at now + 1 minute
or:
或者:
echo b.sh | at -t `date -v+60S "+%Y%m%d%H%M%S"`
-v+60S
adds 60 seconds to current time. You can control exactly how many seconds you want to add.
-v+60S
将当前时间增加 60 秒。您可以准确控制要添加的秒数。
But usually, when people wants one program to launch a minute after the other, they are not 100% sure it will not take more or less than a minute. that's it. b.sh
could be launched before a.sh
is finished. or a.sh
could have finished 30 seconds earlier than "planned" and b.sh could have started faster.
但是通常,当人们希望一个程序在另一个程序之后一分钟启动时,他们并不能 100% 确定不会超过或少于一分钟。就是这样。b.sh
可以在a.sh
完成之前启动。或者a.sh
可能比“计划”提前 30 秒完成并且 b.sh 可能启动得更快。
I would recommend a different model. Where b.sh
is launched first.
a.sh
creates a temp file when it starts. execute is tasks and delete its temp file at the end.
b.sh
watch for the temp file to be created, then deleted. and start its tasks.
我会推荐一个不同的模型。哪里b.sh
先启动。
a.sh
启动时创建一个临时文件。执行是任务并在最后删除其临时文件。
b.sh
注意要创建的临时文件,然后删除。并开始它的任务。
回答by William Pursell
Make the final line of a.sh:
制作 a.sh 的最后一行:
sleep 60 && b.sh
(If b.sh is not in a directory in PATH, make that a full path to b.sh.)
(如果 b.sh 不在 PATH 中的目录中,请将其设为 b.sh 的完整路径。)
回答by larsks
You can just sleep
:
你可以sleep
:
a.sh
sleep 60
b.sh
Or for more complicated cases you can use the at
command:
或者对于更复杂的情况,您可以使用以下at
命令:
echo b.sh | at now + 1 minute
See the at
man page for more information.
有关at
更多信息,请参阅手册页。
回答by Paused until further notice.
If you want to execute the second script some number of seconds after the startof the first script, you can do this in the first:
如果您想在第一个脚本启动后几秒内执行第二个脚本,您可以在第一个脚本中执行此操作:
b.sh &
and this in the second:
这在第二个:
sleep 10
# more commands
You could pass the number of seconds as an argument from the first to the second.
您可以将秒数作为参数从第一个传递到第二个。
Unfortunately, at
doesn't do time increments finer than one minute.
不幸的是,at
时间增量不会超过一分钟。
回答by ankit tyagi
Schedule both the scripts to run at the same time in cron and put the required delay in b.sh.
在 cron 中安排两个脚本同时运行,并将所需的延迟放在 b.sh 中。
回答by David K
You could use the command to sleep your script for 1 minute.
您可以使用该命令让脚本休眠 1 分钟。
sleep 1m
Then when you wish to call the 2nd script
然后当你想调用第二个脚本时
bash a.sh