Check if script is still running in bash

enthuguy
enthuguy used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
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

Author

Commented:
thx arnold

Run this in background as well
sample script pls

Just wondering what if it goes beyond 10000
Distinguished Expert 2017
Commented:
#!/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

Distinguished Expert 2017

Commented:
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?

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial