bash 我怎么能延迟运行shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32359374/
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-09-08 22:05:20  来源:igfitidea点击:

How could I run a shell script with delay

bashshellcommand-lineraspberry-piraspbian

提问by Jorge Cordero

I basically want to run a script which is a server but with 10 second delay, it is because I need some stuff to run before this script.

我基本上想运行一个服务器脚本,但是有 10 秒的延迟,这是因为我需要在这个脚本之前运行一些东西。

The server is located in the folder /etc/init.dbut basically to make it work I go to that path using the command line and I have to restart the server typing:

服务器位于文件夹中,/etc/init.d但基本上是为了使其工作,我使用命令行转到该路径,我必须重新启动服务器,键入:

sudo ./znodejs.sh stop

And then I start the server again:

然后我再次启动服务器:

sudo ./znodejs.sh start

I would like to know if there is any way to run those commands with a delay.

我想知道是否有任何方法可以延迟运行这些命令。

回答by Has QUIT--Anony-Mousse

The standard unix command for sleeping is called

用于睡眠的标准 unix 命令称为

sleep

to wait a second, use

等一下,使用

sleep 1

回答by Chris Montanaro

In order to make a script run on startup first make it executable:

为了使脚本在启动时运行,首先使其可执行:

$ sudo chmod 755 /etc/init.d/znodejs.sh

Then you can register the script to be run at startup:

然后您可以注册要在启动时运行的脚本:

$ sudo update-rc.d znodejs.sh defaults

(Edit) original answer:

(编辑)原始答案:

the sleep command sill pause for a given number of seconds:

sleep 命令会暂停给定的秒数:

sudo ./znodejs.sh stop
sleep 10
sudo ./znodejs.sh start