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

Posted on 2012-09-18
Last Modified: 2012-09-25
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.
Question by:babyb00mer
    LVL 26

    Expert Comment

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

    Open in new window

    LVL 48

    Expert Comment

    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.
    LVL 19

    Accepted Solution

    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!

    Author Closing Comment

    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).

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (, discussed installing the Solaris Operating S…
    Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now