views:

199

answers:

2

I have a shell script that transfers a build.xml file to a remote unix machine (devrsp02) and executes the ANT task wldeploy on that machine (devrsp02). Now, this wldeploy task takes around 15 minutes to complete and while this is running, the last line at the unix console is -

"task {some digit} initialized".

Once this task is complete, we get a "task Completed" msg and the next task in the script is executed only after that.

But sometimes, there might be a problem with the weblogic domain and the deployment might be failing internally, with no effect on the status of the wldeploy task. The unix console will still be stuck at "task {some digit} initialized". The error of the deployment will be getting logged in a file called output.a

So, what I want now is - Start a time counter before running wldeploy. If the wldeploy runs for more than 15 minutes, the following command should be run -

tail -f output.a ## without terminating the wldeploy

or

cat output.a ## after terminating the wldeploy forcefully

Point to be noted here is - I can't run the wldeploy task in background, as in that case the user won't get to know when the task is complete, which is crucial for this script.

Could you please suggest anything to achieve this?

+2  A: 

Create this script (deploy.sh for example):

#!/bin/sh
sleep 900 && pkill -n wldeploy && cat output.a &
wldeploy 

Then from the console

chmod +x deploy.sh

Then run

./deploy.sh

This script will start a counter (15 minutes) that will forcibly kill the wldeploy process if it's running, and if the process was running you'll see the contents of output.a.

If the script has terminated then pkill will not return true and output.a will not be shown.

I would call this task monitoring rather than "parallel processing" :)

Andy
Be careful not to run more than one copy of deploy.sh at once, though.
pra
Hi Andy. This approach would simply terminate the wldeploy after 900 secs. That's not what I wanted. Thanks for replying though.
Bikram Agarwal
Hello Bikram, this will only terminate wldeploy it it's running, and will output the debug you specified.
Andy
+1  A: 

This will only kill the wldeploy process it started, tell you whether wldeploy returned success or failure, and run no more than 30 seconds after wldeploy finishes.

It should be sh-compatible, but the /bin/sh I've got access to now seems to have a broken wait command.

#!/bin/ksh 
wldeploy &
while [ ${slept:-0} -le 900 ]; do
    sleep 30 && slept=`expr ${slept:-0} + 30`
    if [ $$ = "`ps -o ppid= -p $!`" ]; then
        echo wldeploy still running
    else
        wait $! && echo "wldeploy succeeded" || echo "wldeploy failed"
        break
    fi
done
if [ $$ = "`ps -o ppid= -p $!`" ]; then
   echo "wldeploy did not finish in $slept seconds, killing it"
   kill $!
   cat output.a
fi
pra
Hi Pra,This is almost exactly what I was looking for. Used this to formulate my needed code. But I'm facing a weird issue. When I run the script, "wldeploy still running" should be displayed after regular intervals. But sometimes this works, sometime it doesn't.e.g., the 1st time I ran it, it worked fine. 2nd time, the "wldeploy still running" msg didn't come at all. Next execution it again worked; and so on. Couldn't find out why this is happening. :( Any ideas?
Bikram Agarwal
btw, my code is -ant deploy do sleep 60 then echo "Deploy still running. $slept secs" else wait $! then echo "Deploy didn't finish in $slept secs. Here's the log." ant log echo "Press Ctrl+C to kill. Press Enter to continue." read loglimit=`expr ${limit} + 300` deploy_checkfi
Bikram Agarwal
Dennis has found most of the issues with your revised script in your other Q. Replying there.
pra