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