Então desenvolvo meu aplicativo de servidor e alguns aplicativos cliente para ele. Após cada reconstrução eu inicio meu servidor (que tende a carregar todos os serviços por cerca de 2 segundos) e então inicio meus clientes ... Então é isso que tenho em meuroteiro:
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 &
Eu me pergunto se existem alternativas sleepno Bash? Como esperar pelo menos 5 segundos e então até que a atividade da CPU no processo Xcaia para 1% e então iniciar o que eu preciso?
Responder1
Pode ser necessário alterar algumas coisas como os critérios grep e o limite da CPU, mas aqui vai:
#!/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 &
Você também pode alterar o horário de sono, se desejar.


