Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do parallel processing in Unix Shell script?

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?

like image 718
Bikram Agarwal Avatar asked Oct 15 '22 06:10

Bikram Agarwal


2 Answers

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" :)

like image 83
Andy Avatar answered Oct 19 '22 17:10

Andy


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
like image 27
pra Avatar answered Oct 19 '22 16:10

pra