1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#!/bin/bash
VERSION="0.0.1"
function Usage {
cat <<USAGE
Usage:
`basename $0` [-h ] [-r ] [-j nb_jobs ] command arg_list
Optional arguments:
-h: Shows help
-r: Replace asterix * in the command string with argument
-j: Number of cpu cores to use (default 2)
Examples:
`basename $0` somecommand arg1 arg2 arg3
`basename $0` -j 3 \"somecommand -r -p\" arg1 arg2 arg3
`basename $0` -j 6 -r \"convert -scale 50% * small/small_*\" *.jpg"
In case you terminate this script prematurely by pressing CTRL + C, run
${here}/killme.sh to terminate any remaining processes.
USAGE
exit 0
}
function Help {
cat <<HELP
This is a simple wrapper for running processes in parallel. Tested both on
Mac (Darwin) and Linux (CentOS 5).
Usage:
`basename $0` [-h ] [-r ] [-j nb_jobs ] command arg_list
Optional arguments:
-h: Shows this help
-r: Replace asterix * in the command string with argument
-j: Number of cpu cores to use (default 2)
Examples:
`basename $0` somecommand arg1 arg2 arg3
`basename $0` -j 3 \"somecommand -r -p\" arg1 arg2 arg3
`basename $0` -j 6 -r \"convert -scale 50% * small/small_*\" *.jpg"
In case you terminate this script prematurely by pressing CTRL + C, run
${here}/killme.sh to terminate any remaining processes.
--------------------------------------------------------------------------------------
Original script by Kawakamasu:
http://pebblesinthesand.wordpress.com/category/parallel-computing/
Script adapted by:
Brian Avants, Penn Image Computing And Science Laboratory
N.M. van Strien, http://www.mri-tutorial.com | NTNU MR-Center
--------------------------------------------------------------------------------------
HELP
exit 0
}
function queue {
QUEUE="$QUEUE $1"
NUM=$(($NUM+1))
}
function regeneratequeuelinux {
OLDREQUEUE=$QUEUE
QUEUE=""
NUM=0
for PID in $OLDREQUEUE
do
if [ -d /proc/$PID ] ; then
QUEUE="$QUEUE $PID"
NUM=$(($NUM+1))
fi
done
}
function checkqueuelinux {
OLDCHQUEUE=$QUEUE
for PID in $OLDCHQUEUE
do
if [ ! -d /proc/$PID ] ; then
regeneratequeuelinux # at least one PID has finished
break
fi
done
}
function regeneratequeuemac {
OLDREQUEUE=$QUEUE
QUEUE=""
NUM=0
for PID in $OLDREQUEUE
do
whm=` whoami `
num=` ps U $whm | grep -i "${PID} " | wc -l `
if [ $num = 2 ] ; then
QUEUE="$QUEUE $PID"
NUM=$(($NUM+1))
fi
done
}
function checkqueuemac {
OLDCHQUEUE=$QUEUE
for PID in $OLDCHQUEUE
do
whm=` whoami `
num=` ps U $whm | grep -i "${PID} " | wc -l `
if [ $num = 1 ] ; then
regeneratequeuemac # at least one PID has finished
break
fi
done
}
here=`pwd`
NUM=0
QUEUE=""
MAX_NPROC=2 # default
REPLACE_CMD=0 # no replacement by default
# parse command line
if [ $# -eq 0 ]; then # must be at least one arg
Usage >&2
fi
while getopts j:rh OPT; do # "j:" waits for an argument "h" doesnt
case $OPT in
h) Help ;;
j) MAX_NPROC=$OPTARG ;;
r) REPLACE_CMD=1 ;;
\?) Usage >&2 ;;
esac
done
# Main program
echo Using max $MAX_NPROC parallel threads
if [ $MAX_NPROC -eq 1 ] ; then
echo " Dont use pexec to run 1 process at a time. "
echo " In this case, just run in series. "
exit
fi
shift `expr $OPTIND - 1` # shift input args, ignore processed args
COMMAND=$1
shift
# keep list of started processes
echo "#!/bin/bash" >> ${here}/killme.sh
chmod +x ${here}/killme.sh
for INS in $* # for the rest of the arguments
do
# DEFINE COMMAND
if [ $REPLACE_CMD -eq 1 ]; then
CMD=${COMMAND//"*"/$INS}
else
CMD="$COMMAND $INS" #append args
fi
echo "Running $CMD"
eval "$CMD &"
# DEFINE COMMAND END
PID=$!
echo "kill $PID" >> ${here}/killme.sh
queue $PID
osmac=0
osmac="` uname -a | grep Darwin `"
oslin=0
oslin="`uname -a | grep Linux`"
if [ ${#osmac} -ne 0 ]
then
while [ $NUM -ge $MAX_NPROC ]; do
checkqueuemac
sleep 0.5
done
elif [ ${#oslin} -ne 0 ]
then
while [ $NUM -ge $MAX_NPROC ]; do
checkqueuelinux
sleep 0.5
done
fi
done
wait # wait for all processes to finish before exit
rm ${here}/killme.sh
exit 0
|