Link to home
Start Free TrialLog in
Avatar of Evan Cutler
Evan CutlerFlag for United States of America

asked on

how to pause a shell script until process completes

Greetings,
I have a shell script calling another in a loop.

However, I want to make sure the second script is not running prior to calling it again.
How do I do so?

My master script is workmaster.sh
The subscript is SPLMaster.sh

How do I force workmaster.sh to wait until SPLMaster.sh is complete prior to calling it again?

Thanks.
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you putting SPLMaster.sh in the background?  if so, and assuming that $$ is the Process ID, do
    wait $$
which will wait for it to finish.

If you aren't putting it in the background, workmaster.sh won't execute the next statement until SPLMaster.sh finishes.
The main script will automatically wait for the subscript to complete, unless your moving it to the background by putting an ampersand ( & ) at the end of the call.

So if there is no such ampersand anywhere, neither in subscript call contained in the master script nor in a possible call to further sub-subscripts in the second script you're fine.

wmp
Avatar of Evan Cutler

ASKER

yes...it's in the background.
How do I find out if the process is still running?

Thanks.
If you have stored the background process's PID in $bgpid:
    kill -0 $bgpid
will return success if the program is still running (that's kill minus zero)
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why would you force the called script into the background when you're going to wait for its completion anyway?
@wmp to allow you to do other stuff while it is running.
Will you have exactly one instance of the master script running? If not I suggest a temp file with the name of the master script containing the pid of the child process. The master process checks for  a pid in the file, if it finds one, it will not start a new child process.
I built my solution based on this one:

                            while [ $(ps -ef | grep SPLMaster | grep -v "grep" | wc -l) -ge 1 ]
                            do
                                   sleep 1
                           done

That's all I wanted to do.
Thanks, you got me asking the right question.