Link to home
Start Free TrialLog in
Avatar of Edgar Cole
Edgar ColeFlag for United States of America

asked on

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.
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of Tintin
Tintin

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.
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Edgar Cole

ASKER

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