等到服务在 bash-script 中启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21475639/
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
Wait until service starts in bash-script
提问by Dmitry Stril
I've a bash-script that starts some service in background. After this service successfully starts it prints "Server is active" to the stdout. I need to wait until this string appears and then continue executing my script. How can I achieve this?
我有一个 bash 脚本,它在后台启动一些服务。此服务成功启动后,它会将“服务器处于活动状态”打印到标准输出。我需要等到这个字符串出现,然后继续执行我的脚本。我怎样才能做到这一点?
采纳答案by nodakai
I would do in this way.
我会这样做。
./server > /tmp/server-log.txt &
sleep 1
while ! grep -m1 'Server is active' < /tmp/server-log.txt; do
sleep 1
done
echo Continue
Here -m1
tells grep(1)
to quit at the first match.
这里-m1
告诉grep(1)
在第一场比赛中退出。
I veryfied my answer with my toy "service" below:
我用下面的玩具“服务”很好地回答了我的问题:
#! /bin/bash
trap "echo 'YOU killed me with SIGPIPE!' 1>&2 " SIGPIPE
rm -f /tmp/server-output.txt
for (( i=0; i<5; ++i )); do
echo "i==$i"
sleep 1;
done
echo "Server is active"
for (( ; i<10; ++i )); do
echo "i==$i"
sleep 1;
done
echo "Server is shutting down..." > /tmp/server-output.txt
If you replace echo Continue
with echo Continue; sleep 1; ls /tmp/server-msg.txt
, you will see ls: cannot access /tmp/server-output.txt: No such file or directory
which proves the "Continue" action was triggered right after the output of Server is active
.
如果替换echo Continue
为echo Continue; sleep 1; ls /tmp/server-msg.txt
,您将看到ls: cannot access /tmp/server-output.txt: No such file or directory
哪个证明在输出 之后立即触发了“继续”操作Server is active
。
回答by Малъ Скрылевъ
For me to read service's status as to service
app:
让我阅读service
应用程序的服务状态:
$ /sbin/service network status
network.service - Network Connectivity
Loaded: loaded (/lib/systemd/system/network.service; enabled)
Active: active (exited) since Ср 2014-01-29 22:00:06 MSK; 1 day 15h ago
Process: 15491 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
$ /sbin/service httpd status
httpd.service - SYSV: Apache is a World Wide Web server. It is used to serve HTML files and CGI.
Loaded: loaded (/etc/rc.d/init.d/httpd)
Active: activating (start) since Пт 2014-01-31 13:59:06 MSK; 930ms ago
and it can be done with the code:
它可以通过代码完成:
function is_in_activation {
activation=$(/sbin/service "" status | grep "Active: activation" )
if [ -z "$activation" ]; then
true;
else
false;
fi
return $?;
}
while is_in_activation network ; do true; done
回答by Lars Christian Jensen
Use grep -q
. The -q
option makes grep
quiet, and it will exit immediately when the text appears.
使用grep -q
. 该-q
选项使grep
静音,并在文本出现时立即退出。
The command below starts ./some-service
in the background, and blocks until "Server is active" appears on stdout.
下面的命令./some-service
在后台启动,并阻塞直到“服务器处于活动状态”出现在标准输出上。
(./some-service &) | grep -q "Server is active"
回答by BMW
Are you asking redirect stderr to stdout?
您是否要求将 stderr 重定向到 stdout?
./yourscript.sh 2>&1 |grep "Server is active" && echo "continue executing my script"