Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parallel Batch Processing

Status
Not open for further replies.

keziah

Programmer
Dec 20, 2002
10
FR
Hello,

I want to do start some jobs in parallel in my script (bash) and return an error code after the end of all process. I want return an error code set to 1 if one of them failed.

How can i retrieve the error code of each process ?
Because with the wait command i've only the error code of the last process if i do:
wait $pid1 $pid2 $pidx

Some idea ?

Thanks for help

 
Can your jobs write some status in sort of log files you can then analyse ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Here is a reaper which will wait for all child processes
Code:
/* compile this as 
 * gcc -o reaper reaper.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main( void ) {
    int     child_status;
    pid_t   wait_status;
    int     final_status = 0;

    /* wait for all children to exit */
    printf( "Waiting for all children to exit\n" );
    while ( (wait_status = waitpid(-1,&child_status,0)) > 0 ) {
        /* did the child exit normally? */
        if ( WIFEXITED(child_status) ) {
            if ( final_status == 0 ) {
                /* update with the exit status of this child */
                /* but only if all the previous children passed OK */
                final_status = WEXITSTATUS(child_status);
            }
        } else {
            /* a child failed horribly */
            final_status = 2;
        }
        sleep( 1 ); /* take a nap, to allow more children to finish */
    }
    printf("All done, final status is %d\n", final_status );
    return final_status;
}

The shell script looks something like this
Code:
#!/bin/sh
./worker &
./worker &
./worker &
./worker &
./worker &
exec ./reaper
The exec at the end is very important. It replaces the shell, and in doing so, the reaper adopts all the children, and can wait for them properly.

--
 
You can write status of each job in a file and anlyze the result file :
[tt]
rm -f status_file
( ./job1 ; echo $? >> status_file) &
( ./job2 ; echo $? >> status_file) &
( ./job3 ; echo $? >> status_file) &

wait # all child processes

grep -c -v 0 status_file | read errors
if [ "$errors" -ne 0 ] ; then
echo "At least one job failed."
end
[/tt]


Jean Pierre.
 
Thanks all for the answer

The C code is cool but i think the solution of Jean-Pierre will is what i want

Regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top