1 | #!/bin/bash |
---|
2 | |
---|
3 | #----------------------------------------------------------- |
---|
4 | # $Id: distexe_execute.sh 138 2010-05-12 17:34:01Z rosiere $ |
---|
5 | #----------------------------------------------------------- |
---|
6 | |
---|
7 | #-----[ global variable ]----------------------------------- |
---|
8 | declare -i TRAP=0; |
---|
9 | |
---|
10 | #-----[ distexe_execute_usage ]----------------------------- |
---|
11 | function distexe_execute_usage () |
---|
12 | { |
---|
13 | echo "Usage : ${0} path_work file_cmd file_cpt file_host lock_process id"; |
---|
14 | echo "Arguments : "; |
---|
15 | echo " * path_work : directory to execute command"; |
---|
16 | echo " * file_cmd : list of command"; |
---|
17 | echo " * file_cpt : file with the index of next command to execute"; |
---|
18 | echo " * file_process : if file is present, station is running"; |
---|
19 | echo " * lock_process : file to lock process"; |
---|
20 | echo " * id : identification"; |
---|
21 | exit; |
---|
22 | } |
---|
23 | |
---|
24 | #-----[ distexe_execute_test_usage ]------------------------ |
---|
25 | function distexe_execute_test_usage () |
---|
26 | { |
---|
27 | if test ${#} -ne 6; then |
---|
28 | distexe_execute_usage; |
---|
29 | fi; |
---|
30 | |
---|
31 | if test -z "${MORPHEO_SCRIPT}"; then |
---|
32 | echo "Environment variable MORPHEO_SCRIPT is not set"; |
---|
33 | distexe_execute_usage; |
---|
34 | fi; |
---|
35 | |
---|
36 | if test ! -d ${1}; then |
---|
37 | echo "Directory ${1} is invalid"; |
---|
38 | fi; |
---|
39 | } |
---|
40 | |
---|
41 | #-----[ distexe_execute_end ]---------------------------- |
---|
42 | function distexe_execute_end() |
---|
43 | { |
---|
44 | ${MORPHEO_SCRIPT}/lock.sh ${LOCK_PROCESS}; |
---|
45 | rm -f ${FILE_PROCESS}; |
---|
46 | ${MORPHEO_SCRIPT}/unlock.sh ${LOCK_PROCESS}; |
---|
47 | } |
---|
48 | |
---|
49 | #-----[ distexe_execute_no_trap ]------------------------ |
---|
50 | function distexe_execute_no_trap() |
---|
51 | { |
---|
52 | TRAP=1; |
---|
53 | } |
---|
54 | |
---|
55 | #-----[ distexe_execute_trap ]--------------------------- |
---|
56 | function distexe_execute_trap() |
---|
57 | { |
---|
58 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> %%%%%%%%%%%%%%%% HITTTTTTTTTTTTTTTTTT %%%%%%%%%%%%%%%%"; |
---|
59 | |
---|
60 | rm -f ${LOCK_CPT} |
---|
61 | |
---|
62 | distexe_execute_end; |
---|
63 | exit 1; |
---|
64 | } |
---|
65 | |
---|
66 | #-----[ distexe_execute ]----------------------------------- |
---|
67 | function distexe_execute () |
---|
68 | { |
---|
69 | trap distexe_execute_no_trap INT TERM; |
---|
70 | |
---|
71 | # test_usage |
---|
72 | distexe_execute_test_usage ${*}; |
---|
73 | |
---|
74 | local -a COMMAND; |
---|
75 | local -i CPT_OLD; |
---|
76 | local -i CPT; |
---|
77 | local PATH_WORK=${1}; |
---|
78 | local FILE_CMD=${2}; |
---|
79 | local FILE_CPT=${3}; |
---|
80 | local FILE_PROCESS=${4}; |
---|
81 | local LOCK_PROCESS=${5}; |
---|
82 | local ID=${6}; |
---|
83 | local LOCK_CPT="${PATH_WORK}/distexe-cpt-lock"; |
---|
84 | local OUTPUT_FILE_INFO="distexe.info"; |
---|
85 | local OUTPUT_FILE_CMD="distexe.command"; |
---|
86 | local OUTPUT_FILE_OUT="distexe.output"; |
---|
87 | |
---|
88 | echo $$ > ${FILE_PROCESS}; |
---|
89 | |
---|
90 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> pid is $$"; |
---|
91 | # Init CPT if this thread is the first |
---|
92 | ${MORPHEO_SCRIPT}/lock.sh ${LOCK_CPT}; |
---|
93 | if test ! -s ${FILE_CPT}; then |
---|
94 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> create counter file ${FILE_CPT}"; |
---|
95 | echo "0" > ${FILE_CPT}; |
---|
96 | fi; |
---|
97 | ${MORPHEO_SCRIPT}/unlock.sh ${LOCK_CPT}; |
---|
98 | |
---|
99 | trap distexe_execute_trap INT TERM; |
---|
100 | |
---|
101 | if test ${TRAP} -eq 1; then |
---|
102 | distexe_execute_trap; |
---|
103 | fi; |
---|
104 | |
---|
105 | # read, line by line, the command file and write in array |
---|
106 | CPT=0; |
---|
107 | |
---|
108 | while read line; do |
---|
109 | COMMAND[${CPT}]="${line}"; |
---|
110 | CPT=$((${CPT}+1)); |
---|
111 | done < ${FILE_CMD}; |
---|
112 | |
---|
113 | local -i NB_DIGIT=0; |
---|
114 | local -i NB_COMMAND=${#COMMAND[*]}; |
---|
115 | local -i x=${NB_COMMAND}; |
---|
116 | |
---|
117 | while test ${x} -ne 0; do |
---|
118 | NB_DIGIT=$((${NB_DIGIT} + 1)); |
---|
119 | x=$((${x} / 10)); |
---|
120 | done; |
---|
121 | |
---|
122 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> is ready"; |
---|
123 | |
---|
124 | # infinite loop |
---|
125 | CPT_OLD=0; |
---|
126 | CPT=0; |
---|
127 | |
---|
128 | while test 1; do |
---|
129 | # Take a number |
---|
130 | CPT_OLD=${CPT}; |
---|
131 | |
---|
132 | # Read the index, and increase |
---|
133 | ${MORPHEO_SCRIPT}/lock.sh ${LOCK_CPT}; |
---|
134 | CPT=$(cat ${FILE_CPT}); |
---|
135 | echo "$((${CPT}+1))" > ${FILE_CPT}; |
---|
136 | ${MORPHEO_SCRIPT}/unlock.sh ${LOCK_CPT}; |
---|
137 | |
---|
138 | # test if this number is valid |
---|
139 | if test ${CPT} -ge ${NB_COMMAND}; then |
---|
140 | CPT=${NB_COMMAND}; |
---|
141 | fi; |
---|
142 | |
---|
143 | # test if between the cpt_old and cpt, there are a synchronisation command |
---|
144 | |
---|
145 | # local -i CPT_SYNC=${CPT}_OLD; |
---|
146 | # |
---|
147 | # while test ${CPT}_SYNC -lt ${CPT}; do |
---|
148 | # if test -z "${COMMAND[${CPT}_SYNC]}"; then |
---|
149 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> synchronisation [${CPT}_SYNC]"; |
---|
150 | # fi; |
---|
151 | # CPT_SYNC=$((${CPT}_SYNC+1)); |
---|
152 | # done; |
---|
153 | |
---|
154 | # test if this number is valid |
---|
155 | if test ${CPT} -eq ${NB_COMMAND}; then |
---|
156 | break; |
---|
157 | fi; |
---|
158 | |
---|
159 | # Test if command is empty ! |
---|
160 | if test ! -z "${COMMAND[${CPT}]}"; then |
---|
161 | local NUM=$(printf %.${NB_DIGIT}d ${CPT}); |
---|
162 | local MAX=$((${NB_COMMAND}-1)); |
---|
163 | |
---|
164 | # echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> execute command [${NUM}] : ${COMMAND[${CPT}]}"; |
---|
165 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> command [${NUM}/${MAX}] : execute"; |
---|
166 | local PATH_CURRENT=${PWD}; |
---|
167 | |
---|
168 | cd ${PATH_WORK} &> /dev/null; |
---|
169 | mkdir "Task_${NUM}" &> /dev/null; |
---|
170 | cd "Task_${NUM}" &> /dev/null; |
---|
171 | echo "id : ${ID}" > ${OUTPUT_FILE_INFO}; |
---|
172 | echo "host : ${HOSTNAME}" >> ${OUTPUT_FILE_INFO}; |
---|
173 | echo "pid : $$" >> ${OUTPUT_FILE_INFO}; |
---|
174 | echo "date : "$(${MORPHEO_SCRIPT}/date.sh) >> ${OUTPUT_FILE_INFO}; |
---|
175 | echo "#!/bin/bash" > ${OUTPUT_FILE_CMD}; |
---|
176 | echo 'source ${HOME}/.bashrc;' >> ${OUTPUT_FILE_CMD}; |
---|
177 | echo "${COMMAND[${CPT}]}" >> ${OUTPUT_FILE_CMD}; |
---|
178 | chmod +x ${OUTPUT_FILE_CMD}; |
---|
179 | ./${OUTPUT_FILE_CMD} &> ${OUTPUT_FILE_OUT}; |
---|
180 | cd ${PATH_CURRENT} &> /dev/null; |
---|
181 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> command [${NUM}/${MAX}] : done"; |
---|
182 | fi; |
---|
183 | done; |
---|
184 | |
---|
185 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${ID}> is done"; |
---|
186 | |
---|
187 | distexe_execute_end; |
---|
188 | } |
---|
189 | |
---|
190 | #-----[ Corps ]--------------------------------------------- |
---|
191 | distexe_execute ${*}; |
---|