Check if script is still running in bash

Hi Friends,

Have a script which invoke same shell script with different argument in background (usually runs couple of hours)

In above scenarios,
1. would like to check if all the above script execution has finished or not
2. In a loop with sleep/interval?
3. Once all finished, call a function and exit

I dont want to use sleep which increases the overall time for some reason in the script. Instead would like to use grep and check in a loop. pls help what is the best way to achieve this

e.g
ps -ef | grep  backgroundDummyProc | grep -v grep
if [ $? -eq 1 ]
then
    echo "Not running"
else
      echo "running"
fi

Thanks in advance
enthuguyAsked:
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.

arnoldCommented:
Do you want those scripts to run in the background? Run them sequentially and the calling script will wait until the other completes before proceeding.

Without sleep, use a counter I.e. A for loop counting till 10000 between the ps check.

While
Ps
Check status, break out of loop if condition met
For counter
Done
enthuguyAuthor Commented:
thx arnold

Run this in background as well
sample script pls

Just wondering what if it goes beyond 10000
arnoldCommented:
#!/bin/bash
runscript.sh \&
#if you want $! Is the PID of the runscript.sh command
PID=$!
While true; do
ps -ef |grep runscript.sh | grep -v grep
If test $? -eq 0; then
           echo running
          else
           echo not
           Break
fi
For ((i=0;i<=10000;i++)); do
Done



Done

Open in new window

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
arnoldCommented:
What are you after?
You have tasks that a script of yours trigger by cron scheduling or Anacron scheduling, etc.
And you want the same or aseparate script to report on when the scripts it triggers complete?
enthuguyAuthor Commented:
Thanks very much
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
Java

From novice to tech pro — start learning today.