[85] | 1 | #!/bin/bash |
---|
| 2 | |
---|
[82] | 3 | #----------------------------------------------------------- |
---|
| 4 | # $Id: execute.sh 112 2009-03-18 22:36:26Z rosiere $ |
---|
| 5 | #----------------------------------------------------------- |
---|
[2] | 6 | |
---|
[112] | 7 | #-----[ global variable ]----------------------------------- |
---|
[78] | 8 | |
---|
[2] | 9 | #-----[ lock ]---------------------------------------------- |
---|
| 10 | function lock () |
---|
| 11 | { |
---|
[112] | 12 | lockfile -1 ${1}; |
---|
[2] | 13 | } |
---|
| 14 | |
---|
| 15 | #-----[ unlock ]-------------------------------------------- |
---|
| 16 | function unlock () |
---|
| 17 | { |
---|
[112] | 18 | rm -f ${1}; |
---|
[2] | 19 | } |
---|
| 20 | |
---|
[112] | 21 | #-----[ my_date ]------------------------------------------- |
---|
| 22 | function my_date () |
---|
[2] | 23 | { |
---|
[112] | 24 | date +"%F %T"; |
---|
| 25 | } |
---|
[2] | 26 | |
---|
[112] | 27 | #-----[ execute_usage ]------------------------------------- |
---|
| 28 | function execute_usage () |
---|
| 29 | { |
---|
| 30 | echo "Usage : ${0} work_dir file_cmd file_cpt file_cpu"; |
---|
| 31 | echo "Arguments : "; |
---|
| 32 | echo " * work_dir : directory to execute command"; |
---|
| 33 | echo " * file_cmd : list of command"; |
---|
| 34 | echo " * file_cpt : file with the index of next command to execute"; |
---|
| 35 | echo " * file_cpu : tmp file to stock the current index"; |
---|
| 36 | exit; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | #-----[ execute_test_usage ]-------------------------------- |
---|
| 40 | function execute_test_usage () |
---|
| 41 | { |
---|
| 42 | if test ${#} -ne 4; then |
---|
| 43 | execute_usage; |
---|
[2] | 44 | fi; |
---|
| 45 | |
---|
[112] | 46 | if test ! -d ${1}; then |
---|
| 47 | echo "Directory ${1} is invalid"; |
---|
| 48 | fi; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | #-----[ execute ]------------------------------------------- |
---|
| 52 | function execute () |
---|
| 53 | { |
---|
| 54 | # test_usage |
---|
| 55 | execute_test_usage ${*}; |
---|
| 56 | |
---|
| 57 | local -a COMMAND; |
---|
| 58 | local -i CPT_OLD; |
---|
| 59 | local -i CPT; |
---|
| 60 | local WORK_DIR=${1}; |
---|
| 61 | local FILE_CMD=${2}; |
---|
| 62 | local FILE_CPT=${3}; |
---|
| 63 | local FILE_CPU=${4}; |
---|
| 64 | local LOCK_CPT="${WORK_DIR}/.lock-cpt"; |
---|
| 65 | local LOCK_CPU="${WORK_DIR}/.lock-cpu"; |
---|
| 66 | local FILE_CMD="distexe.command"; |
---|
| 67 | local FILE_OUT="distexe.output"; |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | # Init CPT if this thread is the first |
---|
| 71 | lock ${LOCK_CPT}; |
---|
| 72 | if test ! -s ${FILE_CPT}; then |
---|
| 73 | echo "0" > ${FILE_CPT}; |
---|
| 74 | fi; |
---|
| 75 | unlock ${LOCK_CPT}; |
---|
| 76 | |
---|
| 77 | # read, line by line, the command file and write in array |
---|
[2] | 78 | CPT=0; |
---|
| 79 | |
---|
| 80 | while read line; do |
---|
[112] | 81 | COMMAND[${CPT}]="${line}"; |
---|
| 82 | CPT=$((${CPT}+1)); |
---|
| 83 | done < ${FILE_CMD}; |
---|
[2] | 84 | |
---|
[112] | 85 | echo " * <${HOSTNAME}-$$> {"$(my_date)"} is ready"; |
---|
[2] | 86 | |
---|
| 87 | # infinite loop |
---|
| 88 | CPT_OLD=0; |
---|
| 89 | CPT=0; |
---|
| 90 | |
---|
| 91 | while test 1; do |
---|
| 92 | # Take a number |
---|
[112] | 93 | CPT_OLD=${CPT}; |
---|
[2] | 94 | |
---|
[112] | 95 | # Read the index, and increase |
---|
| 96 | lock ${LOCK_CPT}; |
---|
| 97 | CPT=$(cat ${FILE_CPT}); |
---|
| 98 | echo "$((${CPT}+1))" > ${FILE_CPT}; |
---|
| 99 | unlock ${LOCK_CPT}; |
---|
[2] | 100 | |
---|
| 101 | # test if this number is valid |
---|
[112] | 102 | if test ${CPT} -ge ${#COMMAND[*]}; then |
---|
[2] | 103 | CPT=${#COMMAND[*]}; |
---|
| 104 | fi; |
---|
| 105 | |
---|
| 106 | # test if between the cpt_old and cpt, there are a synchronisation command |
---|
| 107 | |
---|
[112] | 108 | # local -i CPT_SYNC=${CPT}_OLD; |
---|
[2] | 109 | # |
---|
[112] | 110 | # while test ${CPT}_SYNC -lt ${CPT}; do |
---|
| 111 | # if test -z "${COMMAND[${CPT}_SYNC]}"; then |
---|
| 112 | # echo " * <${HOSTNAME}-$$> {"$(my_date)"} synchronisation [${CPT}_SYNC]"; |
---|
[2] | 113 | # fi; |
---|
[112] | 114 | # CPT_SYNC=$((${CPT}_SYNC+1)); |
---|
[2] | 115 | # done; |
---|
| 116 | |
---|
| 117 | # test if this number is valid |
---|
[112] | 118 | if test ${CPT} -eq ${#COMMAND[*]}; then |
---|
[2] | 119 | break; |
---|
| 120 | fi; |
---|
| 121 | |
---|
| 122 | # Test if command is empty ! |
---|
[112] | 123 | if test ! -z "${COMMAND[${CPT}]}"; then |
---|
| 124 | local CURREN_DIR=${PWD}; |
---|
| 125 | cd ${WORK_DIR} &> /dev/null; |
---|
| 126 | mkdir "Task_${CPT}" &> /dev/null; |
---|
| 127 | cd "Task_${CPT}" &> /dev/null; |
---|
| 128 | echo " * <${HOSTNAME}-$$> {"$(my_date)"} execute command [${CPT}] : ${COMMAND[${CPT}]}"; |
---|
| 129 | echo "${COMMAND[${CPT}]}" > ${FILE_CMD}; |
---|
| 130 | # chmod +x ${FILE_CMD}; |
---|
| 131 | eval "${COMMAND[${CPT}]}" &> ${FILE_OUT}; |
---|
| 132 | cd ${CURREN_DIR} &> /dev/null; |
---|
[2] | 133 | fi; |
---|
| 134 | done; |
---|
| 135 | |
---|
[112] | 136 | echo " * <${HOSTNAME}-$$> {"$(my_date)"} is done"; |
---|
[2] | 137 | |
---|
[112] | 138 | lock ${LOCK_CPU}; |
---|
| 139 | CPT=$(cat ${FILE_CPU}); # read the index |
---|
| 140 | CPT=$((${CPT}-1)); |
---|
| 141 | echo "${CPT}" > ${FILE_CPU}; # write the next index |
---|
| 142 | unlock ${LOCK_CPU}; |
---|
[2] | 143 | |
---|
[112] | 144 | if test ${CPT} -eq 0; then |
---|
| 145 | echo " * <${HOSTNAME}-$$> {"$(my_date)"} All task is executed"; |
---|
| 146 | rm ${FILE_CPU}; |
---|
[2] | 147 | fi; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | #-----[ Corps ]--------------------------------------------- |
---|
[112] | 151 | execute ${*}; |
---|