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.
LVL 9
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simon3270Commented:
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.
0
woolmilkporcCommented:
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
0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
yes...it's in the background.
How do I find out if the process is still running?

Thanks.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

simon3270Commented:
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)
0
simon3270Commented:
Ah, typo - I meant $! holds the pid of the last backgrounded process, not $$ which is the pid of the parent process.

So
while true
do
  SPLMaster.sh &
  bgpid=$!

  while true
  do
    : Do some work here while SPLMaster.sh is running
    if ! kill -0 $bgpid
    then
      break
    fi
  done
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
woolmilkporcCommented:
Why would you force the called script into the background when you're going to wait for its completion anyway?
0
simon3270Commented:
@wmp to allow you to do other stuff while it is running.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
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.
0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.