Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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