1 | #!/bin/bash |
---|
2 | |
---|
3 | #----------------------------------------------------------- |
---|
4 | # $Id: execute.sh 85 2008-05-14 13:09:48Z rosiere $ |
---|
5 | #----------------------------------------------------------- |
---|
6 | |
---|
7 | declare -a COMMAND; |
---|
8 | declare -i CPT_OLD; |
---|
9 | declare -i CPT; |
---|
10 | declare FILE_CMD=; |
---|
11 | declare FILE_CPT=; |
---|
12 | declare FILE_CPU=; |
---|
13 | declare LOCK_CPT="${HOME}/.lock-cpt"; |
---|
14 | declare LOCK_CPU="${HOME}/.lock-cpu"; |
---|
15 | |
---|
16 | #-----[ lock ]---------------------------------------------- |
---|
17 | function lock () |
---|
18 | { |
---|
19 | lockfile -1 $1; |
---|
20 | } |
---|
21 | |
---|
22 | #-----[ unlock ]-------------------------------------------- |
---|
23 | function unlock () |
---|
24 | { |
---|
25 | rm -f $1; |
---|
26 | } |
---|
27 | |
---|
28 | #-----[ main ]---------------------------------------------- |
---|
29 | function main () |
---|
30 | { |
---|
31 | # no test, because the script execute_n have make all test |
---|
32 | FILE_CMD=$1; |
---|
33 | FILE_CPT=$2; |
---|
34 | FILE_CPU=$3; |
---|
35 | |
---|
36 | lock $LOCK_CPT; |
---|
37 | if test ! -s $FILE_CPT; then |
---|
38 | echo "0" > $FILE_CPT; |
---|
39 | fi; |
---|
40 | unlock $LOCK_CPT; |
---|
41 | |
---|
42 | # lit les fichiers ligne par ligne et le place dans un tableau |
---|
43 | CPT=0; |
---|
44 | |
---|
45 | while read line; do |
---|
46 | COMMAND[$CPT]="$line"; |
---|
47 | CPT=$(($CPT+1)); |
---|
48 | done < $1; |
---|
49 | |
---|
50 | echo " * <$$> {"`date +"%F %T"`"} is ready"; |
---|
51 | |
---|
52 | # infinite loop |
---|
53 | CPT_OLD=0; |
---|
54 | CPT=0; |
---|
55 | |
---|
56 | while test 1; do |
---|
57 | # Take a number |
---|
58 | CPT_OLD=$CPT; |
---|
59 | |
---|
60 | lock $LOCK_CPT; |
---|
61 | CPT=`cat $FILE_CPT`; # read the index |
---|
62 | echo "$(($CPT+1))" > $FILE_CPT; # write the next index |
---|
63 | unlock $LOCK_CPT; |
---|
64 | |
---|
65 | # test if this number is valid |
---|
66 | if test $CPT -ge ${#COMMAND[*]}; then |
---|
67 | CPT=${#COMMAND[*]}; |
---|
68 | fi; |
---|
69 | |
---|
70 | # test if between the cpt_old and cpt, there are a synchronisation command |
---|
71 | |
---|
72 | # local -i CPT_SYNC=$CPT_OLD; |
---|
73 | # |
---|
74 | # while test $CPT_SYNC -lt $CPT; do |
---|
75 | # if test -z "${COMMAND[$CPT_SYNC]}"; then |
---|
76 | # echo " * <$$> {"`date +"%F %T"`"} synchronisation [$CPT_SYNC]"; |
---|
77 | # fi; |
---|
78 | # CPT_SYNC=$(($CPT_SYNC+1)); |
---|
79 | # done; |
---|
80 | |
---|
81 | # test if this number is valid |
---|
82 | if test $CPT -eq ${#COMMAND[*]}; then |
---|
83 | break; |
---|
84 | fi; |
---|
85 | |
---|
86 | # Test if command is empty ! |
---|
87 | if test ! -z "${COMMAND[$CPT]}"; then |
---|
88 | local CUR_DIR=$PWD; |
---|
89 | |
---|
90 | mkdir "Task_$CPT" &> /dev/null; |
---|
91 | cd "Task_$CPT" &> /dev/null; |
---|
92 | echo " * <$$> {"`date +"%F %T"`"} execute command [$CPT] : ${COMMAND[$CPT]}"; |
---|
93 | echo "${COMMAND[$CPT]}" > "command"; |
---|
94 | chmod +x "command"; |
---|
95 | eval "${COMMAND[$CPT]}" &> "output"; |
---|
96 | cd $CUR_DIR &> /dev/null; |
---|
97 | fi; |
---|
98 | done; |
---|
99 | |
---|
100 | echo " * <$$> {"`date +"%F %T"`"} is done"; |
---|
101 | |
---|
102 | lock $LOCK_CPU; |
---|
103 | CPT=`cat $FILE_CPU`; # read the index |
---|
104 | CPT=$(($CPT-1)); |
---|
105 | echo "$CPT" > $FILE_CPU; # write the next index |
---|
106 | unlock $LOCK_CPU; |
---|
107 | |
---|
108 | if test $CPT -eq 0; then |
---|
109 | echo " * <$$> {"`date +"%F %T"`"} All task is executed"; |
---|
110 | rm $FILE_CPU; |
---|
111 | fi; |
---|
112 | } |
---|
113 | |
---|
114 | #-----[ Corps ]--------------------------------------------- |
---|
115 | main $*; |
---|