f



Using "wait" for processes running in the background

Hi all,
      I've got a script (korn shell) that kicks off three processes in
the background. What i eventually want to do, is make my script WAIT
until these three processes have finished in order for the script to
carry on executing. But with no luck.

     I kick off each process, and right after, i store their PIDs in
global variables so that they can be accessed later on by wait
commands, like:

     runMyFirstProcess.ksh &
     FIRST_PROC_PID=$!

     ... (same for the 2 other processes)

     wait FIRST_PROC_PID
     wait SECOND_PROC_PID
     ... (same for third)

     ..carry on executing....

   But the script just flies through...is it ignoring the "wait" calls
because the processes are running in the background?

   I know a work-around way of achieving what i want (i.e., each
process generates a log file, which i can grep in a loop for a return
code or a message) but i'm looking for a much more "neat" solution....

    Can any of you guys shed some light?

    Cheers,

          Gabriel
0
gabrielpacor
10/7/2003 1:07:01 PM
comp.unix.programmer 10848 articles. 0 followers. kokososo56 (350) is leader. Post Follow

12 Replies
4907 Views

Similar Articles

[PageSpeed] 55

On Tue, 07 Oct 2003 at 13:07 GMT, Gabriel Pacor wrote:
> Hi all,
>       I've got a script (korn shell) that kicks off three processes in
> the background. What i eventually want to do, is make my script WAIT
> until these three processes have finished in order for the script to
> carry on executing. But with no luck.
> 
>      I kick off each process, and right after, i store their PIDs in
> global variables so that they can be accessed later on by wait
> commands, like:
> 
>      runMyFirstProcess.ksh &
>      FIRST_PROC_PID=$!
> 
>      ... (same for the 2 other processes)
> 
>      wait FIRST_PROC_PID
>      wait SECOND_PROC_PID
>      ... (same for third)

    That should be:
 
wait $FIRST_PROC_PID
wait $SECOND_PROC_PID

    But all you need is:

wait


wait [N]
    Wait for the specified process and report its termination status.
    If N is not given, all currently active child processes are waited
    for, and the return code is zero.

>      ..carry on executing....
> 
>    But the script just flies through...is it ignoring the "wait" calls
> because the processes are running in the background?
> 
>    I know a work-around way of achieving what i want (i.e., each
> process generates a log file, which i can grep in a loop for a return
> code or a message) but i'm looking for a much more "neat" solution....


-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Chris
10/7/2003 1:28:58 PM
In article <db50db26.0310070507.3f069b00@posting.google.com>,
Gabriel Pacor <gabrielpacor@hotmail.com> wrote:
>Hi all,
>      I've got a script (korn shell) that kicks off three processes in
>the background. What i eventually want to do, is make my script WAIT
>until these three processes have finished in order for the script to
>carry on executing. But with no luck.
>
>     I kick off each process, and right after, i store their PIDs in
>global variables so that they can be accessed later on by wait
>commands, like:
>
>     runMyFirstProcess.ksh &
>     FIRST_PROC_PID=$!
>
>     ... (same for the 2 other processes)
>
>     wait FIRST_PROC_PID
>     wait SECOND_PROC_PID

I don't know if this is your actual problem, but those should be:

     wait $FIRST_PROC_PID
     wait $SECOND_PROC_PID

You were missing the '$' characters.

-- 
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
0
Barry
10/7/2003 2:45:34 PM
Barry Margolin <barry.margolin@level3.com> wrote in message news:<ikAgb.213$pd.84@news.level3.com>...
> In article <db50db26.0310070507.3f069b00@posting.google.com>,
> Gabriel Pacor <gabrielpacor@hotmail.com> wrote:
> >Hi all,
> >      I've got a script (korn shell) that kicks off three processes in
> >the background. What i eventually want to do, is make my script WAIT
> >until these three processes have finished in order for the script to
> >carry on executing. But with no luck.
> >
> >     I kick off each process, and right after, i store their PIDs in
> >global variables so that they can be accessed later on by wait
> >commands, like:
> >
> >     runMyFirstProcess.ksh &
> >     FIRST_PROC_PID=$!
> >
> >     ... (same for the 2 other processes)
> >
> >     wait FIRST_PROC_PID
> >     wait SECOND_PROC_PID
> 
> I don't know if this is your actual problem, but those should be:
> 
>      wait $FIRST_PROC_PID
>      wait $SECOND_PROC_PID
> 
> You were missing the '$' characters.

Sorry about that, that was a mispelling, i've got the $ in front of
the variables, but still...flying through....a colleague told me that
the wait actually waits for a PID to be assigned to the process by the
OS, not the termination
of the process itself, cos it's run in the background and therefore
it's not considered a "child process"...is this true?

Chris, if i do use wait [N], would the syntax be something like:

wait[$FIRST_PROC_PID] 

?

How do i get the return code of the process from invoking wait? 

I can't use JUST wait, because i do need to know how each process
finished
executing, i.e., successful? failed? etc...

Thanx 4 your help,

Gab
0
gabrielpacor
10/7/2003 6:05:04 PM
Gabriel Pacor wrote:
> Sorry about that, that was a mispelling, i've got the $ in front of
> the variables, but still...flying through....a colleague told me that
> the wait actually waits for a PID to be assigned to the process by the
> OS, not the termination
> of the process itself, cos it's run in the background and therefore
> it's not considered a "child process"...is this true?

No on all counts.

Your post showed:
      runMyFirstProcess.ksh &
      FIRST_PROC_PID=$!

Are you trying to wait for runMyFirstProcess.ksh (actually the shell 
that is interpretting it) or something it launches, as the name 
implies.  If it's the latter you can't use wait.  If the script runs 
something and then exits your process's child has exited.  Your wait 
calls will return the status of the script not what it runs.  Your 
process's grandchildren get inherited by init (pid = 1) and you can't 
use wait to wait for them.  If this is your situation you either need 
to have your runMyWhateverProcess.ksh scripts wait as well to keep the 
chain in tact or you need to report status by some other means.

-- ced


-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

0
Chuck
10/7/2003 6:50:54 PM
Chuck Dillon <cdillon@nimblegen.com> wrote in message news:<blv1t6$1um$1@grandcanyon.binc.net>...
> Gabriel Pacor wrote:
> > Sorry about that, that was a mispelling, i've got the $ in front of
> > the variables, but still...flying through....a colleague told me that
> > the wait actually waits for a PID to be assigned to the process by the
> > OS, not the termination
> > of the process itself, cos it's run in the background and therefore
> > it's not considered a "child process"...is this true?
> 
> No on all counts.
> 
> Your post showed:
>       runMyFirstProcess.ksh &
>       FIRST_PROC_PID=$!
> 
> Are you trying to wait for runMyFirstProcess.ksh (actually the shell 
> that is interpretting it) or something it launches, as the name 
> implies.  If it's the latter you can't use wait.  If the script runs 
> something and then exits your process's child has exited.  Your wait 
> calls will return the status of the script not what it runs.  Your 
> process's grandchildren get inherited by init (pid = 1) and you can't 
> use wait to wait for them.  If this is your situation you either need 
> to have your runMyWhateverProcess.ksh scripts wait as well to keep the 
> chain in tact or you need to report status by some other means.
> 
> -- ced

Ok, cheers, i'll be more specific :). 
For testing purposes, i was running test scripts
(namely runMyFirstProcess.ksh for example) that wouldn't launch
anything, in fact, the scripts were just doing a 'sleep 30', cos i
wanted to simulate a process actually doing something which took some
time.
In reality, the processes that i want to run actually run the Adobe
Distiller Server application, which convert Postcript files to PDF
files. Hence, instead of having a script running (namely
runMyFirstProcess.ksh) for which to wait completion for, i would
invoke the Distiller server directly with some files and run it in the
background, i.e., /blabla/blablabla/Distiller blablabla &
Therefore, in answer to your question, no i'm not running a script
that launches something, i'm launching something directly and running
it on the background....
As i said in my previous post, the recommendation of my colleagues is
to do it the "dirty way", and grep the logs that each process
generates until i reach a certain completion message. I don't believe
this is the only solution (i might be wrong - that's why i'm posting
to you guys) and i want a cleaner solution making use of unix process
signalling rather than greping logs...
I hope this helps on what i'm trying to achieve...so, do you think i
can or can't use 'wait' to wait for the completion of my Distiller
processes? If i can, how can i get the return code of the process by
invoking wait later on in the main script?

Cheers,

Gab
0
gabrielpacor
10/7/2003 10:46:09 PM
* gabrielpacor@hotmail.com (Gabriel Pacor)
| Therefore, in answer to your question, no i'm not running a script
| that launches something, i'm launching something directly and
| running it on the background....

This works fine for me (the `ps -ef' depend on the OS):

    #!/bin/sh --

    sleep 5 &
    sleep1=$!

    sleep 3 &
    sleep2=$!

    echo waiting for sleep2...
    wait $sleep2
    echo waiting for sleep2...done

    # sleep 1 should show here
    ps -ef | grep sleep

    echo waiting for sleep1...
    wait $sleep1
    echo waiting for sleep1...done

    ##################
    # same as above, using unspec wait
    sleep 5 &
    sleep1=$!

    sleep 3 &
    sleep2=$!

    # two sleep processes should show up here 
    ps -ef | grep sleep

    echo waiting for sleeps
    wait
    # no sleep processes should show up here
    ps -ef | grep sleep

    # End of file

Does it work for you, too?  If so, I'd check the difference between
the programs you invoke and the plain `sleep' used here.

R'
0
Ralf
10/8/2003 8:57:47 AM
Gabriel Pacor wrote:
> I hope this helps on what i'm trying to achieve...so, do you think i
> can or can't use 'wait' to wait for the completion of my Distiller
> processes? If i can, how can i get the return code of the process by
> invoking wait later on in the main script?
> 

Sure IF the process you call behaves as you want it to.  If the 
distiller process runs serially you should be able to in a modern 
flavor of sh/ksh/bash.

<example script>
#! /bin/ksh

sleep 60 &
s1=$!
sleep 60 &
s2=$!

printf "s1 is %d and s2 is %d\n" $s1 $s2


/usr/bin/kill $s2

wait $s1; s1stat=$?
wait $s2; s2stat=$?

printf "s1(%d) exited with %d\n" $s1 $s1stat
printf "s2(%d) exited with %d cause I killed it\n" $s2 $s2stat

</example script>

<example run>

cdillon@aruba(187)> ./foo.ksh
s1 is 19770 and s2 is 19771
s1(19770) exited with 0
s2(19771) exited with 143 cause I killed it

</example run>

If you continue to have problems I suggest you persue this in 
comp.unix.shell.

-- ced




-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

0
Chuck
10/8/2003 2:17:15 PM
On 7 Oct 2003 06:07:01 -0700, Gabriel Pacor 
  <gabrielpacor@hotmail.com> wrote:
> Hi all,
>       I've got a script (korn shell) that kicks off three processes in
> the background. What i eventually want to do, is make my script WAIT
> until these three processes have finished in order for the script to
> carry on executing. But with no luck.
> 
If you need to wait for these processes, why are they in the background?
Just get rid of all the &'s.


-- 
Cheops' Law:
	Nothing ever gets built on schedule or within budget.
0
Bill
10/8/2003 2:53:25 PM
gabrielpacor@hotmail.com (Gabriel Pacor) writes:

> Hi all,
>       I've got a script (korn shell) that kicks off three processes in
> the background. What i eventually want to do, is make my script WAIT
> until these three processes have finished in order for the script to
> carry on executing. But with no luck.
> 
>      I kick off each process, and right after, i store their PIDs in
> global variables so that they can be accessed later on by wait
> commands, like:
> 
>      runMyFirstProcess.ksh &
>      FIRST_PROC_PID=$!
> 
>      ... (same for the 2 other processes)
> 
>      wait FIRST_PROC_PID
>      wait SECOND_PROC_PID

Perhaps you mean:

       wait $FIRST_PROC_PID
       wait $SECOND_PROC_PID

>     Can any of you guys shed some light?

Otherwise, I use bash and this works well in bash.

-- 
__Pascal_Bourguignon__
http://www.informatimago.com/
Do not adjust your mind, there is a fault in reality.
0
Pascal
10/8/2003 8:58:01 PM
In article <50ld51-cc5.ln1@don.localnet>,
Bill Marcum  <bmarcum@iglou.com> wrote:
>On 7 Oct 2003 06:07:01 -0700, Gabriel Pacor 
>  <gabrielpacor@hotmail.com> wrote:
>> Hi all,
>>       I've got a script (korn shell) that kicks off three processes in
>> the background. What i eventually want to do, is make my script WAIT
>> until these three processes have finished in order for the script to
>> carry on executing. But with no luck.
>> 
>If you need to wait for these processes, why are they in the background?
>Just get rid of all the &'s.

He presumably wants them to be able to run concurrently.  If he doesn't put
them in the background, the second process can't start until the first one
finishes.

-- 
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
0
Barry
10/8/2003 9:03:34 PM
Pascal Bourguignon <spam@thalassa.informatimago.com> wrote in message news:<87llrv4fc6.fsf@thalassa.informatimago.com>...
> gabrielpacor@hotmail.com (Gabriel Pacor) writes:
> 
> > Hi all,
> >       I've got a script (korn shell) that kicks off three processes in
> > the background. What i eventually want to do, is make my script WAIT
> > until these three processes have finished in order for the script to
> > carry on executing. But with no luck.
> > 
> >      I kick off each process, and right after, i store their PIDs in
> > global variables so that they can be accessed later on by wait
> > commands, like:
> > 
> >      runMyFirstProcess.ksh &
> >      FIRST_PROC_PID=$!
> > 
> >      ... (same for the 2 other processes)
> > 
> >      wait FIRST_PROC_PID
> >      wait SECOND_PROC_PID
> 
> Perhaps you mean:
> 
>        wait $FIRST_PROC_PID
>        wait $SECOND_PROC_PID
> 
> >     Can any of you guys shed some light?
> 
> Otherwise, I use bash and this works well in bash.

Guys, yes exactly, i need to run them concurrently in the background,
not foreground, cos as Barry correctly says, the 2nd process can't
start til the first one finishes and so on...
I'm using korn shell and have to stick to that cos that's what we use
in production, so, although i have tried other shells myself i can't
by-pass that one :)
0
gabrielpacor
10/9/2003 10:04:25 AM
Chuck Dillon <cdillon@nimblegen.com> wrote in message news:<bm167m$krb$1@grandcanyon.binc.net>...
> Gabriel Pacor wrote:
> > I hope this helps on what i'm trying to achieve...so, do you think i
> > can or can't use 'wait' to wait for the completion of my Distiller
> > processes? If i can, how can i get the return code of the process by
> > invoking wait later on in the main script?
> > 
> 
> Sure IF the process you call behaves as you want it to.  If the 
> distiller process runs serially you should be able to in a modern 
> flavor of sh/ksh/bash.
> 
> <example script>
> #! /bin/ksh
> 
> sleep 60 &
> s1=$!
> sleep 60 &
> s2=$!
> 
> printf "s1 is %d and s2 is %d\n" $s1 $s2
> 
> 
> /usr/bin/kill $s2
> 
> wait $s1; s1stat=$?
> wait $s2; s2stat=$?
> 
> printf "s1(%d) exited with %d\n" $s1 $s1stat
> printf "s2(%d) exited with %d cause I killed it\n" $s2 $s2stat
> 
> </example script>
> 
> <example run>
> 
> cdillon@aruba(187)> ./foo.ksh
> s1 is 19770 and s2 is 19771
> s1(19770) exited with 0
> s2(19771) exited with 143 cause I killed it
> 
> </example run>
> 
> If you continue to have problems I suggest you persue this in 
> comp.unix.shell.
> 
> -- ced

Chuck, cheers for that, your example in korn shell works fine for
me....can't really see what i was doing wrong before....but thanks
again. I'll try now and run my Distiller rather than the 'sleep's.....
Thanks again,
Gab
0
gabrielpacor
10/9/2003 10:24:31 AM
Reply:

Web resources about - Using "wait" for processes running in the background - comp.unix.programmer

Resources last updated: 3/4/2016 8:44:08 AM