#!/bin/bash # This is a sample program by Bryan Lee ( leebc11 at acm dot org ) that runs mul tiple commands in parallel in bash. # Distributed under GPL version 2 # Let me know if you find it useful # Arguments should be specified on the initial command line. They will be used as the first argument for the parallel command one at a time. #This changes how bash handles process notification. It MUST be set. set -bm startprocess () { if [ $1 ] then echo Processing file $1 xv -quit $1 1>/dev/null & # This is the parallel command # OR #xview $1 & # Or this without IO redirection else echo "Out of files" echo "Waiting for remaining processes to finish" wait # This wait waits for everything else echo "All done...Bye, bye." exit 255 fi } #This starts a new process each time a child exits trap 'startprocess $1; shift' SIGCHLD echo Starting with $# arguments #Have one of these for each parallel process you want running startprocess $1; shift startprocess $1; shift startprocess $1; shift startprocess $1; shift wait exit ############### # How it works # # When a child process exits, it sends a signal back to the parent, SIGCHLD. # The "trap" command catches this signal and calls the startProcessing # function with the next filename. The function checks to see that it # was passed anything, and waits, then exits if it was not passed anything. # I use built in shell functionality to process the arguments. The first # argument to the script is $1, then $2, then $3.... The "shift" command # shifts these arguments, $2 becomes $1, $3 becomes $2, $4 becomes $3, etc. # This is the typical method for processing arguments in a script. Each # time the startProcessing function is called, you have to shift. # The "set -bm" changes shell behavior. # The "wait" command waits for child processes to finish before # proceeding. The wait in the function before the exit (which exits the # entire script) makes sure the children finish before exiting. # # If you don't give it enough arguments initially, you'll get two "Out of # files" messages, and the whole thing will probably fail if there's any # whitespace in the arguments.