等到服务以bash-script开始

时间:2021-07-11 07:41:33

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脚本,可以在后台启动一些服务。成功启动此服务后,它会将“Server is active”打印到stdout。我需要等到这个字符串出现,然后继续执行我的脚本。我怎样才能做到这一点?

3 个解决方案

#1


8  

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;睡1; ls /tmp/server-msg.txt,您将看到ls:无法访问/tmp/server-output.txt:没有这样的文件或目录证明在Server的输出处于活动状态后立即触发了“Continue”操作。

#2


1  

For me to read service's status as to service app:

让我阅读服务应用程序的服务状态:

$ /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 "$1" status | grep "Active: activation" )
   if [ -z "$activation" ]; then
      true;
   else
      false;
   fi

   return $?;
}

while is_in_activation network ; do true; done

#3


0  

Are you asking redirect stderr to stdout?

你问stderr重定向到stdout吗?

./yourscript.sh  2>&1 |grep "Server is active" && echo "continue executing my script"

#1


8  

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;睡1; ls /tmp/server-msg.txt,您将看到ls:无法访问/tmp/server-output.txt:没有这样的文件或目录证明在Server的输出处于活动状态后立即触发了“Continue”操作。

#2


1  

For me to read service's status as to service app:

让我阅读服务应用程序的服务状态:

$ /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 "$1" status | grep "Active: activation" )
   if [ -z "$activation" ]; then
      true;
   else
      false;
   fi

   return $?;
}

while is_in_activation network ; do true; done

#3


0  

Are you asking redirect stderr to stdout?

你问stderr重定向到stdout吗?

./yourscript.sh  2>&1 |grep "Server is active" && echo "continue executing my script"