How to do parallel processing in Unix Shell script? How to do parallel processing in Unix Shell script? unix unix

How to do parallel processing in Unix Shell script?


Create this script (deploy.sh for example):

#!/bin/shsleep 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" :)


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    fidoneif [ $$ = "`ps -o ppid= -p $!`" ]; then   echo "wldeploy did not finish in $slept seconds, killing it"   kill $!   cat output.afi


For the part without terminating the wldeploy it is easy, just execute before

  { sleep 900; tail -f output.a; } &

For the part with kill it, it is more complex, as you have determine the PID of the wldeploy process. The answer of pra is exactly doing that, so I would just refer to that.