??? How do I wait for a non-child process completion?

??? How do I wait for a non-child process completion?

Post by Slava Bogdanovi » Sun, 14 Feb 1999 04:00:00



Hi,
A process initiated by a command with & at the end in shell script
should wait for completion of the process which started it. How this
could be done? ("wait()" waits only for child processes). How do I
find the pid to wait for?

Thanks.

 
 
 

??? How do I wait for a non-child process completion?

Post by Tom Keat » Sun, 14 Feb 1999 04:00:00



> Hi,
> A process initiated by a command with & at the end in shell script
> should wait for completion of the process which started it. How this
> could be done? ("wait()" waits only for child processes). How do I
> find the pid to wait for?

> Thanks.

I will try to help, but I find your question ambiguous.

If you are stating that child processes wait for their parents,
i'm afraid you've got it backwards.

In shell scripts "wait" is typically used to stall processing (of
the script) until the backgrounded command returns.

If on the other hand, your problem is to resume a suspended child
process when its parent (shell script) terminates, something can be
approximated with kill(1) and trap, as per:

#!/bin/sh
trap 'kill -CONT $cmdPID' 0

keypress=' '
cmdPID=

# something that takes a while to run
#
alias cmd="find / -name '*.a' -print"

cmd&

# $! refers to the PID of most recently backgrounded process.
# The construct:  wait $!  in shell scripts is common.
#
cmdPID=$!

# Signals are _not_ "job control",
# although they can be used to control jobs.  In other words,
# doing fg or bg in above trap won't work.
#
kill -STOP $cmdPID

while [ "$keypress" != q ]
do
   read -p "enter q to quit " keypress
 done

# trap catches here
#
exit

cheers,
        tom

-- Why not just drop everything and go fishin'?

        remove NO_SPAM. from address to reply