1 | #!/bin/bash |
---|
2 | |
---|
3 | #----------------------------------------------------------- |
---|
4 | # $Id: distexe.sh 124 2009-06-17 12:11:25Z rosiere $ |
---|
5 | #----------------------------------------------------------- |
---|
6 | |
---|
7 | VERSION="1.0" |
---|
8 | |
---|
9 | # Need : test, echo, cd, dirname, basename, ssh |
---|
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 | |
---|
43 | #-----[ my_date ]------------------------------------------- |
---|
44 | function my_date () |
---|
45 | { |
---|
46 | date +"%F %T"; |
---|
47 | } |
---|
48 | |
---|
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; |
---|
77 | } |
---|
78 | |
---|
79 | #-----[ header ]-------------------------------------------- |
---|
80 | function header () |
---|
81 | { |
---|
82 | echo "distexe ${VERSION}"; |
---|
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 | |
---|
95 | # Absolute path of work directory |
---|
96 | local PATH_EXE; |
---|
97 | if test ${#} -eq 2; then |
---|
98 | cd ${2} &> /dev/null; |
---|
99 | PATH_EXE=${PWD}; |
---|
100 | cd - &> /dev/null; |
---|
101 | else |
---|
102 | PATH_EXE=${PWD}; |
---|
103 | fi; |
---|
104 | |
---|
105 | header; |
---|
106 | echo " * {"$(my_date)"} <${HOSTNAME}> file : ${FILE_CMD}"; |
---|
107 | echo " * {"$(my_date)"} <${HOSTNAME}> path : ${PATH_EXE}"; |
---|
108 | |
---|
109 | local hosts="${DISTEXE_HOSTS}"; |
---|
110 | |
---|
111 | for line in ${hosts}; do |
---|
112 | local host=$(echo ${line} | cut -d/ -f1); |
---|
113 | local -i nb_process=$(echo ${line} | cut -d/ -f2); |
---|
114 | |
---|
115 | echo " * {"$(my_date)"} <${HOSTNAME}> station : ${host} (${nb_process}) ... "; |
---|
116 | |
---|
117 | # lunch service |
---|
118 | local cmd="export MORPHEO_SCRIPT=${MORPHEO_SCRIPT};${MORPHEO_SCRIPT}/execute_n.sh ${PATH_EXE} ${FILE_CMD} ${nb_process};"; |
---|
119 | ssh ${host} ${cmd} & |
---|
120 | done; |
---|
121 | |
---|
122 | echo " * {"$(my_date)"} <${HOSTNAME}> all hosts working"; |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | #-----[ Corps ]--------------------------------------------- |
---|
127 | distexe ${*}; |
---|