23

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?

2
  • 1
    Don't do that. Make your script a service itself. Then it may depend if you use systemd or plain old SysV init.... Commented Jan 31, 2014 at 8:45
  • 1
    Or even just make a lock file when it starts, or check pid, or something else, no reason to capture stdout here. Commented Jan 31, 2014 at 8:47

4 Answers 4

16

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

11

Use grep -q. The -q option makes grep quiet, and it will exit immediately when the text appears.

The command below starts ./some-service in the background, and blocks until "Server is active" appears on stdout.

(./some-service &) | grep -q "Server is active" 

1 Comment

This is awesome! I did something like this: /opt/mssql/bin/sqlservr > /root/sqlservr.out & and then tail -f /root/sqlservr.out | grep -q "SQL Server is now ready"
5

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 

Comments

0

Are you asking redirect stderr to stdout?

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.