| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | #----------------------------------------------------------- |
|---|
| 4 | # $Id: distexe.sh 128 2009-06-26 08:43:23Z rosiere $ |
|---|
| 5 | #----------------------------------------------------------- |
|---|
| 6 | |
|---|
| 7 | declare VERSION="1.0" |
|---|
| 8 | declare -i SLEEP=3 |
|---|
| 9 | |
|---|
| 10 | # Need : test, echo, cd, dirname, basename, ssh, ps aux |
|---|
| 11 | |
|---|
| 12 | #-----[ distexe_usage ]------------------------------------- |
|---|
| 13 | function distexe_usage () |
|---|
| 14 | { |
|---|
| 15 | echo "Usage : ${0} file [ dir ]"; |
|---|
| 16 | echo "Arguments : "; |
|---|
| 17 | echo " * file : list of command"; |
|---|
| 18 | echo " * dir : work directory (default is current directory)."; |
|---|
| 19 | # echo " * nb_process : number of process (default (and maximum) is the number of processor)"; |
|---|
| 20 | echo ""; |
|---|
| 21 | echo "Note : "; |
|---|
| 22 | echo " * File content list of command. Each line is execute by an host."; |
|---|
| 23 | echo " A line can content many shell command."; |
|---|
| 24 | echo " Don't forgot the final end of line (else the last command is not executed)"; |
|---|
| 25 | echo " example : "; |
|---|
| 26 | echo ' echo "export PATH=${PATH}:${HOME}/my_appli/bin; my_appli 13" > command_file.txt'; |
|---|
| 27 | echo ' echo "export PATH=${PATH}:${HOME}/my_appli/bin; my_appli 21" >> command_file.txt'; |
|---|
| 28 | echo ' echo "export PATH=${PATH}:${HOME}/my_appli/bin; my_appli 7" >> command_file.txt'; |
|---|
| 29 | echo ' echo "" >> command_file.txt'; |
|---|
| 30 | echo " * For each command, a directory (in work directory) is created : Task_X (X is the number of line in command file)."; |
|---|
| 31 | echo " The command is execute in this directory. (Warning, set your environment !)."; |
|---|
| 32 | echo " Two file is generate : \"distexe.output\" is the output (standard and error) of the execution, and \"distexe.command\" is the command lunch."; |
|---|
| 33 | # echo " * A command empty (no command on a line of file) is a synchronisation with all process" |
|---|
| 34 | echo " * The environment variable DISTEXE_HOSTS list hosts to execute command"; |
|---|
| 35 | echo " After each host, a number is to the number of process. It's optionnal, the default value is the number of processor."; |
|---|
| 36 | echo " example :"; |
|---|
| 37 | echo ' export DISTEXE_HOSTS="localhost/1 nod/4 gdi/2"'; |
|---|
| 38 | echo " * All hosts must have network file systems (per example nfs or samba)"; |
|---|
| 39 | echo " Work directory must be in this network file systems !"; |
|---|
| 40 | echo ""; |
|---|
| 41 | exit; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | #-----[ my_date ]------------------------------------------- |
|---|
| 45 | function my_date () |
|---|
| 46 | { |
|---|
| 47 | date +"%F %T"; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | #-----[ distexe_test_usage ]-------------------------------- |
|---|
| 51 | function distexe_test_usage () |
|---|
| 52 | { |
|---|
| 53 | if test ${#} -ne 1 -a ${#} -ne 2; then |
|---|
| 54 | distexe_usage; |
|---|
| 55 | fi; |
|---|
| 56 | |
|---|
| 57 | if test -z "${MORPHEO_SCRIPT}"; then |
|---|
| 58 | echo "Environment variable MORPHEO_SCRIPT is not set"; |
|---|
| 59 | distexe_usage; |
|---|
| 60 | fi; |
|---|
| 61 | |
|---|
| 62 | if test ! -f ${1}; then |
|---|
| 63 | echo "File \"${1}\" is invalid"; |
|---|
| 64 | distexe_usage; |
|---|
| 65 | fi; |
|---|
| 66 | |
|---|
| 67 | if test ! -s ${1}; then |
|---|
| 68 | echo "File \"${1}\" is empty."; |
|---|
| 69 | distexe_usage; |
|---|
| 70 | fi; |
|---|
| 71 | |
|---|
| 72 | if test ${#} -eq 2; then |
|---|
| 73 | if test ! -d ${2}; then |
|---|
| 74 | echo "Directory \"${2}\" is invalid."; |
|---|
| 75 | distexe_usage; |
|---|
| 76 | fi; |
|---|
| 77 | fi; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | #-----[ header ]-------------------------------------------- |
|---|
| 81 | function header () |
|---|
| 82 | { |
|---|
| 83 | echo "distexe ${VERSION}"; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | #-----[ distexe ]------------------------------------------- |
|---|
| 87 | function distexe () |
|---|
| 88 | { |
|---|
| 89 | distexe_test_usage ${*}; |
|---|
| 90 | |
|---|
| 91 | # construct file command with absolute path |
|---|
| 92 | cd $(dirname ${1}); |
|---|
| 93 | local FILE_CMD=${PWD}/$(basename ${1}); |
|---|
| 94 | cd -; |
|---|
| 95 | |
|---|
| 96 | # Absolute path of work directory |
|---|
| 97 | local PATH_EXE; |
|---|
| 98 | if test ${#} -eq 2; then |
|---|
| 99 | cd ${2} &> /dev/null; |
|---|
| 100 | PATH_EXE=${PWD}; |
|---|
| 101 | cd - &> /dev/null; |
|---|
| 102 | else |
|---|
| 103 | PATH_EXE=${PWD}; |
|---|
| 104 | fi; |
|---|
| 105 | |
|---|
| 106 | local FILE_CPT="${PATH_EXE}/control-"$(basename ${FILE_CMD}); |
|---|
| 107 | |
|---|
| 108 | header; |
|---|
| 109 | |
|---|
| 110 | echo " * {"$(my_date)"} <${HOSTNAME}> file : ${FILE_CMD}"; |
|---|
| 111 | echo " * {"$(my_date)"} <${HOSTNAME}> path : ${PATH_EXE}"; |
|---|
| 112 | echo " * {"$(my_date)"} <${HOSTNAME}> sleep : ${SLEEP}"; |
|---|
| 113 | |
|---|
| 114 | local hosts="${DISTEXE_HOSTS}"; |
|---|
| 115 | local -a commands; |
|---|
| 116 | local -i cpt=0; |
|---|
| 117 | |
|---|
| 118 | for line in ${hosts}; do |
|---|
| 119 | local host=$(echo ${line} | cut -d/ -f1); |
|---|
| 120 | local -i nb_process=$(echo ${line} | cut -d/ -f2); |
|---|
| 121 | |
|---|
| 122 | echo " * {"$(my_date)"} <${HOSTNAME}> station : ${host} (${nb_process}) ... "; |
|---|
| 123 | |
|---|
| 124 | # lunch service |
|---|
| 125 | local cmd="export MORPHEO_SCRIPT=${MORPHEO_SCRIPT};${MORPHEO_SCRIPT}/execute_n.sh ${PATH_EXE} ${FILE_CMD} ${FILE_CPT} ${nb_process};"; |
|---|
| 126 | ssh ${host} ${cmd} & |
|---|
| 127 | |
|---|
| 128 | commands[${cpt}]="${cmd}"; |
|---|
| 129 | cpt=$((${cpt}+1)); |
|---|
| 130 | done; |
|---|
| 131 | |
|---|
| 132 | echo " * {"$(my_date)"} <${HOSTNAME}> all hosts working"; |
|---|
| 133 | |
|---|
| 134 | cpt=0; |
|---|
| 135 | while test ${cpt} -lt ${#commands[*]}; do |
|---|
| 136 | local -i res=1 |
|---|
| 137 | |
|---|
| 138 | while true; do |
|---|
| 139 | res=$(ps aux | grep -c "${commands[${cpt}]}"); |
|---|
| 140 | |
|---|
| 141 | if test ${res} -eq 0; then |
|---|
| 142 | break; |
|---|
| 143 | fi; |
|---|
| 144 | |
|---|
| 145 | # wait (to not have 100% cpu) |
|---|
| 146 | sleep ${SLEEP}; |
|---|
| 147 | done |
|---|
| 148 | |
|---|
| 149 | cpt=$((${cpt}+1)); |
|---|
| 150 | done; |
|---|
| 151 | |
|---|
| 152 | echo " * {"$(my_date)"} <${HOSTNAME}> all hosts is done"; |
|---|
| 153 | rm ${FILE_CPT}; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | #-----[ Corps ]--------------------------------------------- |
|---|
| 157 | distexe ${*}; |
|---|