[85] | 1 | #!/bin/bash |
---|
| 2 | |
---|
[82] | 3 | #----------------------------------------------------------- |
---|
| 4 | # $Id: execute_n.sh 85 2008-05-14 13:09:48Z rosiere $ |
---|
| 5 | #----------------------------------------------------------- |
---|
[2] | 6 | |
---|
| 7 | #-----[ variable ]------------------------------------------ |
---|
| 8 | declare -a COMMAND; |
---|
| 9 | declare -i NB_PROCESS; |
---|
| 10 | declare -i CPT; |
---|
| 11 | declare FILE_CMD; |
---|
| 12 | declare FILE_CPT; |
---|
| 13 | declare FILE_CPU; |
---|
[78] | 14 | declare ID="cpu-${HOSTNAME}-$$" |
---|
[2] | 15 | |
---|
| 16 | #-----[ usage ]--------------------------------------------- |
---|
| 17 | function usage () |
---|
| 18 | { |
---|
[78] | 19 | echo "Usage : $0 file [ nb_process ]"; |
---|
| 20 | echo "Arguments : "; |
---|
| 21 | echo " * file : list of command"; |
---|
| 22 | echo " * nb_process : number of process (default (and maximum) is the number of processor)"; |
---|
[2] | 23 | echo ""; |
---|
[78] | 24 | echo "Note : "; |
---|
| 25 | echo " * This script, for each command, create a directory : Task_X (X is the number of command), and execute the command in this directory."; |
---|
| 26 | echo " * Two file is generate : \"output\" is the output of the execution, and \"command\" is the command lunch."; |
---|
| 27 | echo " * A command empty (no command on a line of file) is a synchronisation with all process" |
---|
| 28 | echo " * Don't forgot the final end of line (else the last command is not executed"; |
---|
[2] | 29 | echo ""; |
---|
| 30 | exit; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | #-----[ nb_cpu ]-------------------------------------------- |
---|
| 34 | function nb_cpu () |
---|
| 35 | { |
---|
| 36 | local FILE_CPUINFO=/proc/cpuinfo; |
---|
| 37 | if test ! -f $FILE_CPUINFO; then |
---|
[78] | 38 | echo "\"${FILE_CPUINFO}\" don't exist." |
---|
[2] | 39 | usage; |
---|
| 40 | fi; |
---|
| 41 | |
---|
[78] | 42 | eval "$1=`grep -c \"processor\" ${FILE_CPUINFO}`"; |
---|
[2] | 43 | } |
---|
| 44 | |
---|
| 45 | #-----[ test_usage ]---------------------------------------- |
---|
| 46 | function test_usage () |
---|
| 47 | { |
---|
| 48 | if test $# -ne 1 -a $# -ne 2; then |
---|
| 49 | usage; |
---|
| 50 | fi; |
---|
| 51 | |
---|
| 52 | if test ! -f $1; then |
---|
| 53 | echo "File \"$1\" don't exist"; |
---|
| 54 | usage; |
---|
| 55 | fi; |
---|
| 56 | |
---|
| 57 | if test ! -s $1; then |
---|
| 58 | echo "File \"$1\" is empty"; |
---|
| 59 | usage; |
---|
| 60 | fi; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | #-----[ main ]---------------------------------------------- |
---|
| 64 | function main () |
---|
| 65 | { |
---|
| 66 | nb_cpu NB_PROCESS; |
---|
| 67 | |
---|
| 68 | test_usage $*; |
---|
| 69 | |
---|
| 70 | if test $# -eq 2; then |
---|
| 71 | if test $2 -lt $NB_PROCESS; then |
---|
| 72 | NB_PROCESS=$2; |
---|
| 73 | fi; |
---|
| 74 | fi; |
---|
| 75 | |
---|
| 76 | FILE_CMD=$1; |
---|
| 77 | FILE_CPT="$HOME/control-"`basename $1`; |
---|
| 78 | FILE_CPU="$HOME/$ID"; |
---|
| 79 | |
---|
| 80 | echo "<$0>" ; |
---|
| 81 | echo " * Initialisation ...." ; |
---|
| 82 | echo " - host name : $HOSTNAME" ; |
---|
| 83 | echo " - number of process : $NB_PROCESS" ; |
---|
| 84 | |
---|
| 85 | local -i IT_NB_PROCESS=1; |
---|
| 86 | local -i PID=$$; |
---|
| 87 | |
---|
| 88 | echo "$NB_PROCESS" > $FILE_CPU; |
---|
| 89 | |
---|
| 90 | # create the same number of thread that processor |
---|
| 91 | while test $IT_NB_PROCESS -le $NB_PROCESS; do |
---|
| 92 | execute.sh $FILE_CMD $FILE_CPT $FILE_CPU & |
---|
| 93 | IT_NB_PROCESS=$(($IT_NB_PROCESS+1)); |
---|
| 94 | |
---|
| 95 | if test "$$" -ne $PID; then |
---|
| 96 | break; |
---|
| 97 | fi; |
---|
| 98 | done |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | #-----[ Corps ]--------------------------------------------- |
---|
| 102 | main $*; |
---|