wait command is ignored or not working when the shell script is called through cron

0

we have requirement to monitor oem agent status , script is working fine when execute sh .sh for getting oem agent status it requires 55s to get the output written to file , I have used that command to execute backend and wait till that command completes but it is not working when the script is scheduled thru cron , it's not waiting till that command completes

Appreciate any help in this regard

below are crontab entries

########################OEM Script Monitoring#################################
0,10,20,30,40,50 * * * * /export/home/oraoem/scripts/emck.sh  > /dev/null 2>&1

-- cat emck.sh
#!/bin/bash
cd /export/home/oraoem/scripts
DBALIST="ramesh.mathur@zap.com"; export DBALIST
rm -f agent.exist
ps -ef | grep emagent | grep -v grep  > agent.exist
if [ -s agent.exist ]
then
cd $MY_AGENT_HOME/bin
./emctl status agent > /export/home/oraoem/scripts/agent.txt & echo $!
wait $!

grep "Number of XML files pending upload" /export/home/oraoem/scripts/agent.txt > /export/home/oraoem/scripts/agent6.txt
a=$(awk '{if ($8 >0) print "", $8}' /export/home/oraoem/scripts/agent6.txt)
b=$(grep "Agent is Running and Ready" /export/home/oraoem/scripts/agent.txt)
d=("Agent is Running and Ready")
if [ "$b" != "$d" ]
then
echo "OEM Agent is Not running ... .. " | mail -s "OEM Agent down on `hostname`" $DBALIST
elif [[ $a -gt 3 ]]
then
echo "Number of XML files pending upload : $a" | mail -s "OEM Agent upload problem on `hostname`" $DBALIST
fi
else
echo "OEM Agent down on `hostname`." | mail -s " OEM Agent down on `hostname`" $DBALIST
fi

##echo "OEM Agent is up and running on  `hostname`." | mail -s " OEM Agent up and running on `hostname`" $DBALIST

RameshMathur Mulagaleti

Posted 2016-07-26T11:50:28.480

Reputation: 1

Answers

-1

Try to replace "wait" by "sleep"

sebastienvg

Posted 2016-07-26T11:50:28.480

Reputation: 186

1This will not work as expected. wait $! means wait for the last background task to finish. sleep $! means wait for a number of seconds equal to the PID of the last background task. Given a PID can be really high (above 20k), you really don't want to run this command. – Nathan.Eilisha Shiraini – 2016-07-26T13:39:09.200

yeah its taking more time and what i observer is below command , emctl status agent will take 50 sec time to generate output from cron its not waiting till the output is written to agent.txt file but same script will run fine when i execute manually sh emck.sh any idea on this – RameshMathur Mulagaleti – 2016-07-27T09:06:40.617