Wait for one processes to complete in a shell script

Let's say I start process A.sh, then start process B.sh. I call both of them in my C.sh
How can I make sure that B starts its execution only after A.sh finishes.
I have to do this in loop.Execution time of A.sh may vary everytime.
It is a parameterized script.

Unless you run a.sh with a '&', it should wait for a.sh to complete to start b.sh. you may also give a sleep time after a.sh and b.sh just to be sure.

Post your script snippet for better answers.

for i in `cat sql_output.txt`
do
echo "bip $i is running"
cd $SCRIPT_DIR
sh bip.sh 01 3 $i > /arbor/integ_fx/rahul_raj/itsr_5652/bip.txt
sleep 90
BILL_REF=`tail -2  /arbor/integ_fx/rahul_raj/itsr_5652/bip.txt | head -1 | cut -d  " " -f  6`
cd /arbor/integ_fx/xit_dev_CH/bin
ksh runXitInvoice_PROFORMA_integ  $BILL_REF 0 bp > /arbor/integ_fx/rahul_raj/itsr_5652/PROFORMA_integ.txt
done

I think bip.sh is designed to run in background.
Current workaround i am using is sleep but i want to avoid it.
Thanks

This is the third thread with your identical problem. Please share bip.sh ! In above code snippet, bip.sh DOES NOT RUN in background!

Hi Rudi,
Just shared the BIP in my other post

---------- Post updated at 05:28 PM ---------- Previous update was at 12:21 PM ----------

Hi here is the content of bip

#!/bin/sh
ARBOR_DB_PASSWD=`cat $ARBORDIR/.arborpw`; export ARBOR_DB_PASSWD
DB_PASS=$ARBOR_DB_PASSWD; export DB_PASS;
ORACLE_SID=$ARBOR_CATALOG_DATABASE; export ORACLE_SID;
ARBORCTRLRPT03=$ARBORDATA/reports/ctrl; export ARBORCTRLRPT03;

Usage(){
  echo "\n\n  Usage is: `basename $0` <proc_num> <bip mode> <account_no>\n"
  echo "  where <proc_num> is a number between 01 and 99"
  echo "        <bip_mode> is a number. Use 0=production, 3=proforma, 6=backout"
  echo "        <bip_mode> is an arbor accout number\n\n"
  exit 0
}

#  Check number of arguments
if [ "$#" -ne 3 ] ; then
  Usage
fi

# Check to see if ARBORDBU is set
if [ -z "${ARBORDBU}" ] ; then
        echo "ERROR: \$ARBORDBU environment variable is not set\n"
        echo "This script requires that the \$ARBORDBU environment variable be set.\n\n"
        exit 1
fi

# Check to see if DB_PASS is set
if [ -z "${DB_PASS}" ] ; then
        echo "ERROR: \$DB_PASS environment variable is not set\n"
        echo "This script requires that the \$DB_PASS environment variable be set.\n\n"
        exit 1
fi

# Check to see if ORACLE_SID is set
if [ -z "${ORACLE_SID}" ] ; then
        echo "ERROR: \$ORACLE_SID environment variable is not set\n"
        echo "This script requires that the \$ORACLE_SID environment variable be set.\n\n"
        exit 1
fi

# Set the variables for arguments passed by the user
PROCNAME=bip$1
BIP_MODE=$2
ACCOUNT=$3

# Get the database from the user's environment
DB=$ORACLE_SID

# Set this so that the process doesn't try to connect to Operations Center
OAM_ENV_CONN_MA=FALSE
export OAM_ENV_CONN_MA


# Log into sqlplus, delete any existing entries, and make the new entry
sqlplus -s $ARBORDBU/$DB_PASS@$DB <<END

update SYSTEM_PARAMETERS set int_value=1 where module='BIP' and parameter_name='TRA_SWITCH';
delete from PROCESS_SCHED where process_name = '$PROCNAME';
delete from PROCESS_STATUS where process_name = '$PROCNAME';
insert into PROCESS_SCHED values('$PROCNAME','$PROCNAME','N',$BIP_MODE,SYSDATE,86400,0,2,55,'$DB','CMF.account_no in ($ACCOUNT)',1,NULL,0,NULL,0);
commit;
exit

END

echo "Starting BIP in the background with process name = \"$PROCNAME\""
BIP $PROCNAME 3 &

Write the entire part below in your c.sh. This should solve your problem. Small modifictions may be required depending upon necessity

function mySleep
{
ps -fu $LOGNAME|grep a.sh|grep -v grep 2>>/dev/null
if [ $? -ne 0 ]
then
echo "a.sh still running...."
echo "sleeping for 2 seconds!!!"
sleep 2
mySleep
}

a.sh
mySleep
b.sh

1 Like

Thanks for the reply.

Can you explain what your code is doing.I am new to unix

Chances are you can wait for that BG program, as it is in then same job tree/group as your main script. Run ps , grep for the (expanded) BIP $PROCNAME 3 process, and wait for its PID.

you could also do this in c.sh ...

a.sh && b.sh

This shouldn't work. You can only "wait" for your children; not your grandchildren or siblings. Since the last thing bip.sh does before exiting is invoke BIP asynchronously, bip.sh returns just after BIP has been started without waiting for it to finish and leaving the process that invoked bip.sh with no way to reap the grandchild's exit status and no direct way to wait for the grandchild to finish.

The obvious fix here is to remove the & at the end of the last line in bip.sh and invoke bip.sh with an & in cases where you want to continue other processing while BIP runs. The shell that invokes bip.sh can then wait for bip.sh to complete using a wait command without operands. But, of course, this may require modifying every other script that calls bip.sh and doesn't want to wait for it to finish.

Similarly, Just Ice's suggestion to use:

bip.sh && c.sh

won't work if the intent is to wait for the BIP command that was started by bip.sh to finish before starting c.sh because the current version of bip.sh returns without waiting for BIP to finish.

1 Like

since bip.sh output is redirected to a file on your main script, you may check if any process has this file open to ensure that the file is written. If no process has it open, the next command ( tail) can be started..

@Don Cragun: Right, I should have known better / read the man.
Still you could find the PID using ps, and then check for the existence of your BIP process. Or, have bip.sh report that PID back.

Yes, bip.sh could report the PID back to the caller, but the caller still can't use the wait utility to wait for it to terminate. The caller could repeatedly search ps command output to determine when the grandchild has terminated. But an invocation of wait with the PID of the grandchild as an operand will yield an exit code of 127 from wait indicating that the grandchild is not known to the current shell (i.e., the underlying waitpid() call generated an ECHILD error).

well...i go into a loop and check if a process named a.sh is still going on..if its still running, i wait for two seconds and again check as the same function is being called within itself...ps gives you the list of processes running and grep displays from the standard output the process named a.sh..if there is no process running, it will start the second process and so on...u can google a bit for the other contents used in the script. just let me know if it helped

In this specific case, if bip.sh invokes BIP just before exiting, a ps loop looking for bip.sh would fail. The ps loop would have to be looking for BIP or whatever BIP might exec (or fork and exec) instead of looking for bip.sh .

The idea behind your approach is sound, but it requires intimate knowledge about what the grandchild is doing so that the tests can be made reliable for any particular application. Also, your approach only works if there can only be one instance of the process (or processes) started by the grandchild running on your system at a time.

So what can i do i can use sleep too but i dont want to use sleep as it would degrade performance.

Assuming that you have three scripts "a.sh" "b.sh" "c.sh" and there are three log file "a.log" "b.log" "c.log", and here is my opinion:
Execution of b.sh depends on whether the last successful execution of a.sh. If execution of a.sh fails, it's unnecessary to execute b.sh;
If you agree that, here is my main workflow:

  1. print a flag into a.log representing successful execution of a.sh.
  2. before execution of b.sh, check whether a.sh executed successfully last time by using the flag in a.log
  3. if a.sh executed successfully, execute b.sh
    Here is my scripts:
    a.sh
#!/bin/bash
start_exec_time=$(date +%Y%m%d%H%M%S) #This is the exact time when script a.sh starts to execute.
echo "Start at:$start_exec_time"
i=0
while [ $i -lt 10 ]
do
        echo "The script a.sh is being executed"
        sleep 1
        i=$(expr $i + 1)
done
fini_exec_time=$(date +%Y%m%d%H%M%S)  #This is the exact time when script a.sh finishes  executing.
echo "Finish at:$fini_exec_time"

b.sh

#!/bin/bash
echo "Start to execute the script b.sh"
echo "The script b.sh is being executed"
echo "Finish executing the script b.sh"

c.sh

#!/bin/bash
i=0
while [ $i -lt 10 ]
do
        ./a >> a.log
        if tail -1 a.log | grep -q '^Finish'
        then
                cur_flag=$(tail -1 a.log | gawk -F: '{print $2}')
                if [ "x${pre_flag}" = "x${cur_flag}" ]
                then
                        echo "script b.sh is unnecessary to be executed."
                        exit
                else
                        ./b >> b.log
                fi
        fi
        pre_flag=$cur_flag
        i=$(expr $i + 1)
done
now_time=$(date +%Y%m%d%H%M%S)
echo "Finish at:$now_time" >> c.log
cp a.log a.log.$now_time
cp b.log b.log.$now_time
rm -rf a.log b.log

Hope to help you

sleep does not degrade performance, in fact it makes least possible use of system ressources. If you can find the PID of the BIP process by means described above, you could try sth like

while [ -d /proc/6024 ]; do sleep 5; done &

, then do what you like, and then wait for the while loop. I'm sure this will be far more efficient than repeatedly checking (=opening, searching) a log file.