Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making linux "Wait" command wait for ALL child processes

Tags:

linux

bash

wait

Wait is not waiting for all child processes to stop. This is my script:

#!/bin/bash

titlename=`echo "$@"|sed 's/\..\{3\}$//'`
screen -X title "$titlename"

/usr/lib/process.bash -verbose $@

wait

bash -c "mail.bash $@"
screen -X title "$titlename.Done"

I don't have access to /usr/lib/process.bash, but it is a script that changes frequently, so I would like to reference it... but in that script:

#!/bin/ksh
#lots of random stuff
/usr/lib/runall $path $auto $params > /dev/null 2>&1&

My problem is that runall creates a log file... and mail.bash is suppose to mail me that log file, but the wait isn't waiting for runall to finish, it seems to only be waiting for process.bash to finish. Is there anyway, without access to process.bash, or trying to keep my own up to date version of process.bash, to make the wait properly wait for runall to finish? (The log file overwrites the previous run, so I can't just check for the presence of the log file, since there is always one there)

Thanks, Dan

like image 686
Dan Avatar asked Feb 18 '10 16:02

Dan


People also ask

Does wait () wait for all child processes?

A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction.

How do you make your parents wait for all child processes?

Just use: while(wait(NULL) > 0); This ensures that you wait for ALL the child processes and only when all have returned, you move to the next instruction.

What happens if you use wait () in the child?

Suspends the calling process until any one of its child processes ends. More precisely, wait() suspends the calling process until the system obtains status information on the ended child. If the system already has status information on a completed child process when wait() is called, wait() returns immediately.

What does wait () do in Linux?

wait is an inbuilt command in the Linux shell. It waits for the process to change its state i.e. it waits for any running process to complete and returns the exit status.


3 Answers

(
    . /usr/lib/process.bash -verbose $@
    wait
)

Instead of letting the OS start process.bash, this creates a subshell, runs all the commands in process.bash as if they were entered into our shell script, and waits within that subshell.

There are some caveats to this, but it should work if you're not doing anything unusual.

like image 76
ephemient Avatar answered Sep 26 '22 00:09

ephemient


wait only waits for direct children; if any children spawn their own children, it won't wait for them.

like image 20
Ignacio Vazquez-Abrams Avatar answered Sep 23 '22 00:09

Ignacio Vazquez-Abrams


The main problem is that because process.bash has exited the runall process will be orphaned and owned by init (PID 1). If you look at the process list runall won't have any visible connection to your process any more since the intermediate process.bash script exited. There's no way to use ps --ppid or anything similar to search for this "grandchild" process once it's orphaned.

You can wait on a specific PID. Do you know the PID of the runall process? If there's only one such process you could try this, which will wait for all running runalls:

wait `pidof runall`
like image 21
John Kugelman Avatar answered Sep 24 '22 00:09

John Kugelman