1 | #!/bin/bash |
---|
2 | |
---|
3 | #----------------------------------------------------------- |
---|
4 | # $Id: distexe_execute_n.sh 138 2010-05-12 17:34:01Z rosiere $ |
---|
5 | #----------------------------------------------------------- |
---|
6 | |
---|
7 | #-----[ global variable ]----------------------------------- |
---|
8 | declare -i TRAP=0; |
---|
9 | |
---|
10 | #-----[ distexe_execute_n_usage ]--------------------------- |
---|
11 | function distexe_execute_n_usage () |
---|
12 | { |
---|
13 | echo "Usage : ${0} path_work file_cmd file_cpt file_host lock_host prefix_file_process sleep [ nb_process ]"; |
---|
14 | echo "Arguments : "; |
---|
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)"; |
---|
23 | echo ""; |
---|
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."; |
---|
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)"; |
---|
29 | echo ""; |
---|
30 | exit; |
---|
31 | } |
---|
32 | |
---|
33 | #-----[ distexe_execute_n_test_usage ]---------------------- |
---|
34 | function distexe_execute_n_test_usage () |
---|
35 | { |
---|
36 | if test ${#} -ne 7 -a ${#} -ne 8; then |
---|
37 | distexe_execute_n_usage; |
---|
38 | fi; |
---|
39 | |
---|
40 | if test -z "${MORPHEO_SCRIPT}"; then |
---|
41 | echo "Environment variable MORPHEO_SCRIPT is not set"; |
---|
42 | distexe_execute_n_usage; |
---|
43 | fi; |
---|
44 | |
---|
45 | if test ! -d ${1}; then |
---|
46 | echo "Directory ${1} is invalid"; |
---|
47 | fi; |
---|
48 | |
---|
49 | if test ! -f ${2}; then |
---|
50 | echo "File \"${2}\" don't exist"; |
---|
51 | distexe_execute_n_usage; |
---|
52 | fi; |
---|
53 | |
---|
54 | if test ! -s ${2}; then |
---|
55 | echo "File \"${2}\" is empty"; |
---|
56 | distexe_execute_n_usage; |
---|
57 | fi; |
---|
58 | } |
---|
59 | |
---|
60 | #-----[ distexe_execute_n_wait_end ]------------------------ |
---|
61 | function distexe_execute_n_wait_end () |
---|
62 | { |
---|
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); |
---|
132 | local -i CPT; |
---|
133 | local PATH_WORK=${1}; |
---|
134 | local FILE_CMD=${2}; |
---|
135 | local FILE_CPT=${3}; |
---|
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}; |
---|
142 | |
---|
143 | distexe_execute_n_test_usage ${*}; |
---|
144 | |
---|
145 | if test ${#} -eq 8; then |
---|
146 | if test ${8} -lt ${NB_PROCESS}; then |
---|
147 | NB_PROCESS=${8}; |
---|
148 | fi; |
---|
149 | fi; |
---|
150 | |
---|
151 | echo " * {"$(${MORPHEO_SCRIPT}/date.sh)"} <${HOSTNAME}> ${NB_PROCESS} process"; |
---|
152 | |
---|
153 | local -i IT_NB_PROCESS=1; |
---|
154 | local -i PID=$$; |
---|
155 | |
---|
156 | # create the same number of thread that processor |
---|
157 | while test ${IT_NB_PROCESS} -le ${NB_PROCESS}; do |
---|
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}" & |
---|
162 | IT_NB_PROCESS=$((${IT_NB_PROCESS}+1)); |
---|
163 | |
---|
164 | if test "$$" -ne ${PID}; then |
---|
165 | break; |
---|
166 | fi; |
---|
167 | done |
---|
168 | |
---|
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; |
---|
179 | } |
---|
180 | |
---|
181 | #-----[ Corps ]--------------------------------------------- |
---|
182 | distexe_execute_n ${*}; |
---|