How to make the parent process to wait for the child process

Hi All,

I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program.

I want 1st script to wait until the 'C' program completes.

I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second script(child script) to complete

Can any one help me, how to get the process id for the 'C' program in unix shell script or is there any other way to make the parent script to wait for the child process.

Thanks in advance.
sennidurai

Just 'wait'. Or don't put the C program in the background.

i have use wait, wait $PID and then wait $!. but the process will not wait for the child process.

Where did you use wait? Only the parent process can wait for any child, so if you start the program in the second script, you'll have to wait for it in the second script, not the first.

Another way would be to save the PID in a temporary file, which is then read in the first script. Then the first script checks the process list until it's no longer listed, eg:

while [ "$( ps -p ${PID} >/dev/null 2>&1; echo $?)" == "0" ]
do
    sleep 1
done

yes it will help, our script is running in production and we dont have the permission to edit the second script (ie. C program). we have to achieve this thru first script only.

Is there any other way to do this.

Thanks,
Sennidurai.

Just to clear this up:

  • You're calling a shell script
  • This is calling a second shell script
  • This second script calls a program written in C, and puts it in the background
  • You're not allowed to change the second script
  • You want to wait for the program to finish in the first script

Right so far?

yes pludi you are right

If the second script doesn't save the PID anywhere, and you're not allowed to change that, your only chance would be to get the PID from a process listing, eg

ps -ef | awk '/cron/ && !/awk/ {print $2}'

assuming that the C program has a unique enough name and that there's always only one instance running. You can then wait for that PID using the code posted above