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.
Who is Participating?
simon3270Connect With a Mentor Commented:
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
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
    waitcount=`expr $waitcount + 1`

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

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!
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_CSESSION=`ps -ef | grep ' $PID_OF_BOURNE '| awk '{print $2}' | head -1`

Open in new window

There is something you aren't telling us.

If you have

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.
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).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.