[85] | 1 | #!/bin/bash |
---|
| 2 | |
---|
[82] | 3 | #----------------------------------------------------------- |
---|
| 4 | # $Id: distexe_execute_n.sh 138 2010-05-12 17:34:01Z rosiere $ |
---|
| 5 | #----------------------------------------------------------- |
---|
[2] | 6 | |
---|
[138] | 7 | #-----[ global variable ]----------------------------------- |
---|
| 8 | declare -i TRAP=0; |
---|
[2] | 9 | |
---|
[138] | 10 | #-----[ distexe_execute_n_usage ]--------------------------- |
---|
| 11 | function distexe_execute_n_usage () |
---|
[2] | 12 | { |
---|
[138] | 13 | echo "Usage : ${0} path_work file_cmd file_cpt file_host lock_host prefix_file_process sleep [ nb_process ]"; |
---|
[78] | 14 | echo "Arguments : "; |
---|
[138] | 15 | echo " * path_work : directory to execute command"; |
---|
| 16 | echo " * file_cmd : list of command"; |
---|
| 17 | echo " * file_cpt : file to control"; |
---|
| 18 | echo " * file_host ; file host"; |
---|
| 19 | echo " * lock_host ; file lock host"; |
---|
| 20 | echo " * prefix_file_process : prefix of process's file"; |
---|
| 21 | echo " * sleep : time in second to sleep"; |
---|
| 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."; |
---|
[112] | 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 | |
---|
[138] | 33 | #-----[ distexe_execute_n_test_usage ]---------------------- |
---|
| 34 | function distexe_execute_n_test_usage () |
---|
[2] | 35 | { |
---|
[138] | 36 | if test ${#} -ne 7 -a ${#} -ne 8; then |
---|
| 37 | distexe_execute_n_usage; |
---|
[2] | 38 | fi; |
---|
| 39 | |
---|
[138] | 40 | if test -z "${MORPHEO_SCRIPT}"; then |
---|
| 41 | echo "Environment variable MORPHEO_SCRIPT is not set"; |
---|
| 42 | distexe_execute_n_usage; |
---|
| 43 | fi; |
---|
| 44 | |
---|
[112] | 45 | if test ! -d ${1}; then |
---|
| 46 | echo "Directory ${1} is invalid"; |
---|
| 47 | fi; |
---|
[2] | 48 | |
---|
[112] | 49 | if test ! -f ${2}; then |
---|
| 50 | echo "File \"${2}\" don't exist"; |
---|
[138] | 51 | distexe_execute_n_usage; |
---|
[2] | 52 | fi; |
---|
| 53 | |
---|
[112] | 54 | if test ! -s ${2}; then |
---|
| 55 | echo "File \"${2}\" is empty"; |
---|
[138] | 56 | distexe_execute_n_usage; |
---|
[2] | 57 | fi; |
---|
| 58 | } |
---|
| 59 | |
---|
[138] | 60 | #-----[ distexe_execute_n_wait_end ]------------------------ |
---|
| 61 | function distexe_execute_n_wait_end () |
---|
[2] | 62 | { |
---|
[138] | 63 | while true; do |
---|
| 64 | |
---|
| 65 | nb_file=$(ls -1 ${PREFIX_FILE_PROCESS}* 2>/dev/null| wc -l); |
---|
| 66 | |
---|
| 67 | if test ${nb_file} -eq 0; then |
---|
| 68 | break; |
---|
| 69 | fi; |
---|
| 70 | |
---|
| 71 | # wait (to not have 100% cpu) |
---|
| 72 | sleep ${SLEEP}; |
---|
| 73 | done; |
---|
| 74 | |
---|
| 75 | ${MORPHEO_SCRIPT}/lock.sh ${LOCK_HOST}; |
---|
| 76 | rm -f ${FILE_HOST}; |
---|
| 77 | ${MORPHEO_SCRIPT}/unlock.sh ${LOCK_HOST}; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | #-----[ distexe_execute_n_no_trap ]------------------------- |
---|
| 81 | function distexe_execute_n_no_trap() |
---|
| 82 | { |
---|
| 83 | TRAP=1; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | #-----[ distexe_execute_n_trap ]---------------------------- |
---|
| 87 | function distexe_execute_n_trap () |
---|
| 88 | { |
---|
| 89 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> Trap signal detected"; |
---|
| 90 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> 1111111111111111 Trap signal detected 1111111111111111"; |
---|
| 91 | |
---|
| 92 | local -a files_process=($(ls ${PREFIX_FILE_PROCESS}* 2>/dev/null)); |
---|
| 93 | local -i nb_files_process=${#files_process[*]}; |
---|
| 94 | |
---|
| 95 | if test ${nb_files_process} -ne 0; then |
---|
| 96 | for file_process in ${files_process[*]}; do |
---|
| 97 | # ssh can not set yet pid in file_process |
---|
| 98 | local -i nb_word=0; |
---|
| 99 | while test ${nb_word} -eq 0; do |
---|
| 100 | nb_word=$(echo ${file_process} |wc -m); |
---|
| 101 | done; |
---|
| 102 | |
---|
| 103 | ${MORPHEO_SCRIPT}/lock.sh ${LOCK_PROCESS}; |
---|
| 104 | |
---|
| 105 | if test -f ${file_process}; then |
---|
| 106 | pid=$(cat ${file_process}); |
---|
| 107 | |
---|
| 108 | #ps aux | grep distexe; |
---|
| 109 | |
---|
| 110 | kill -s SIGINT ${pid}; |
---|
| 111 | fi; |
---|
| 112 | |
---|
| 113 | ${MORPHEO_SCRIPT}/unlock.sh ${LOCK_PROCESS}; |
---|
| 114 | done; |
---|
| 115 | fi; |
---|
| 116 | |
---|
| 117 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> 2222222222222222 Trap signal detected 2222222222222222"; |
---|
| 118 | |
---|
| 119 | distexe_execute_n_wait_end; |
---|
| 120 | |
---|
| 121 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> 3333333333333333 Trap signal detected 3333333333333333"; |
---|
| 122 | |
---|
| 123 | exit 1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | #-----[ distexe_execute_n_main ]---------------------------- |
---|
| 127 | function distexe_execute_n () |
---|
| 128 | { |
---|
| 129 | trap distexe_execute_n_no_trap INT TERM; |
---|
| 130 | |
---|
| 131 | local -i NB_PROCESS=$(${MORPHEO_SCRIPT}/nb_cpu.sh); |
---|
[112] | 132 | local -i CPT; |
---|
[124] | 133 | local PATH_WORK=${1}; |
---|
[112] | 134 | local FILE_CMD=${2}; |
---|
[126] | 135 | local FILE_CPT=${3}; |
---|
[138] | 136 | local FILE_HOST=${4}; |
---|
| 137 | local LOCK_HOST=${5}; |
---|
| 138 | local PREFIX_FILE_PROCESS="${6}${HOSTNAME}-"; |
---|
| 139 | local LOCK_PROCESS="${PREFIX_FILE_PROCESS}lock"; |
---|
| 140 | local -i SLEEP=${7}; |
---|
| 141 | echo $$ > ${FILE_HOST}; |
---|
[2] | 142 | |
---|
[138] | 143 | distexe_execute_n_test_usage ${*}; |
---|
[2] | 144 | |
---|
[138] | 145 | if test ${#} -eq 8; then |
---|
| 146 | if test ${8} -lt ${NB_PROCESS}; then |
---|
| 147 | NB_PROCESS=${8}; |
---|
[124] | 148 | fi; |
---|
[2] | 149 | fi; |
---|
| 150 | |
---|
[138] | 151 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> ${NB_PROCESS} process"; |
---|
[2] | 152 | |
---|
| 153 | local -i IT_NB_PROCESS=1; |
---|
| 154 | local -i PID=$$; |
---|
| 155 | |
---|
| 156 | # create the same number of thread that processor |
---|
[112] | 157 | while test ${IT_NB_PROCESS} -le ${NB_PROCESS}; do |
---|
[138] | 158 | local FILE_CPU=${PREFIX_FILE_PROCESS}${IT_NB_PROCESS}; |
---|
| 159 | |
---|
| 160 | touch ${FILE_CPU}; |
---|
| 161 | ${MORPHEO_SCRIPT}/distexe_execute.sh ${PATH_WORK} ${FILE_CMD} ${FILE_CPT} ${FILE_CPU} ${LOCK_PROCESS} "${HOSTNAME}-${IT_NB_PROCESS}" & |
---|
[112] | 162 | IT_NB_PROCESS=$((${IT_NB_PROCESS}+1)); |
---|
[2] | 163 | |
---|
[112] | 164 | if test "$$" -ne ${PID}; then |
---|
[2] | 165 | break; |
---|
| 166 | fi; |
---|
| 167 | done |
---|
[124] | 168 | |
---|
[138] | 169 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> all process working"; |
---|
| 170 | |
---|
| 171 | trap distexe_execute_n_trap INT TERM; |
---|
| 172 | |
---|
| 173 | if test ${TRAP} -eq 1; then |
---|
| 174 | distexe_execute_n_trap; |
---|
| 175 | fi; |
---|
| 176 | |
---|
| 177 | # Wait end ok all Task |
---|
| 178 | distexe_execute_n_wait_end; |
---|
[2] | 179 | } |
---|
| 180 | |
---|
| 181 | #-----[ Corps ]--------------------------------------------- |
---|
[138] | 182 | distexe_execute_n ${*}; |
---|