1

So I develop my server app and some client apps for it. After each rebuild I start my server (which tends to load qup all services for something like 2 seconds) and then I start my clients... So his is what I have in my script:

cd $RUN_DIR
nohup ./CloudServer >& /dev/null &
sleep 5
nohup ./CloudClient --server=localhost --username=$ROBOT1_NAME --robot >& /dev/null &
nohup ./CloudClient --server=localhost --username=$ROBOT2_NAME --robot >& /dev/null &

I wonder are there any alternatives for sleep in Bash? Like wait at least 5 seconds and then until CPU activety on process X will go down to 1% and then launch what I need?

  • 11
    The "sleep to give the server time to get ready" is a really really bad idea. It would be far better if your client and server were both robust enough to handle the case where the other end isn't ready to talk to them, using retries or whatever. – Paul Tomblin Mar 15 '12 at 15:16
2

You may need to change some things like the grep criteria and the CPU threshold but here it goes:

#!/bin/bash
cd $RUN_DIR
nohup ./CloudServer >& /dev/null &
PID=`ps aux |grep $RUN_DIR/CloudServer|grep -v grep| head -n 1 |awk '{print $2}'`

while [ `top -n 1 -b -p $PID | grep $PID |awk '{print $9"/1"}' |bc` -gt 1 ]
do
        sleep 2
        echo Server still starting up ...
done
echo Server is now Idle
nohup ./CloudClient --server=localhost --username=$ROBOT1_NAME --robot >& /dev/null &
nohup ./CloudClient --server=localhost --username=$ROBOT2_NAME --robot >& /dev/null &

You may change the sleep time, if you wish, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.