• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1412
  • Last Modified:

How to make a Korn shell script wait for a background job to finish

I have a Korn shell script which calls a Bourne shell script which runs a Cache database process. From memory, the Cache command looks something  like this:

csession TEST -U something goes here ^ZBKCRON

The calling order looks like this:

Korn-shell-script --> Bourne-shell-script --> Cache-command

The problem is that I need for the Korn shell script to wait for the csession command to finish before it terminates. Unfortunately, it does not. I've tried using the wait command, to no avail. Under the circumstances, I don't think the $! variable would be of much use. The shell's ampersand operator (&) is not being used to put the csession command in the background, so if that's what's happening, I have no idea how.
0
babyb00mer
Asked:
babyb00mer
1 Solution
 
arober11Commented:
Unless the Bourne shell has forked (nohup) the csession process it should exist as the parent, until the csession process dies, if this is the case simply wait for the bourne shell to complete.

Alternatively if the csession process has been forked, the cleanest solution would be to alter the bourne shell to return the PID of the backgrounded csession, which you could trap and wait for, failing that the forked process will have the bourne shell's PID as it's PPID, so just find and wait for a process with that PID to end e.g.

bash  xxxxxxxx  &
PID_OF_BOURNE=$!
wait $PID_OF_BOURNE

#FRIG
PID_OF_CSESSION=`ps -ef | grep ' $PID_OF_BOURNE '| awk '{print $2}' | head -1`
wait $PID_OF_CSESSION

Open in new window

0
 
TintinCommented:
There is something you aren't telling us.

If you have

#!/bin/bash
csession TEST -U something goes here ^ZBKCRON
echo End

Open in new window


then the bash script won't output 'End' until the cession script has finished.
0
 
simon3270Commented:
It sounds as though the csession program is forking and detaching from its parent.

If you know the name of the cache process which is running, you can run "ps" and look for it, then wait for it to finish.

This script times out after 100 3-second waits (=300 seconds, i.e. 5 minutes) - adjust to taste.

For example:
csession TEST -U something goes here ^ZBKCRON
waitcount=0
progStr="program_string "   # The name to match the background program

while [ $waitcount -lt 100 ]; do
    sleep 3
    if ps -ef | grep -v grep | grep "$progStr" >/dev/null
    then
        break
    fi
    waitcount=`expr $waitcount + 1`
done 

if [ $waitcount -eq 100 ]; then
    echo Timed out waiting for process $progStr to complete
fi

Open in new window


There are some steps here which could be made more efficient (in particular to avoid the "grep -v grep", and the $waitcount increment), but "simple" is probably better here!
0
 
babyb00merAuthor Commented:
This was difficult to implement, because the corresponding processes were difficult to identify and isolate. Under the circumstances, however, it was the only viable solution. The primary difference between this example and what I ultimately implemented is that I dispensed with the timeout (i.e., the waitcount).
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now