-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch
executable file
·156 lines (130 loc) · 2.89 KB
/
batch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/sh
trap "" TERM
if [ $# -ne 1 ]
then
cat <<-tac
USAGE: $0 <filename>
<filename> names a job list file, each line of which consists of a set of arguments to the driver script.
NB: "arguments to the driver script" means you should NOT start the lines with '`dirname "$0"`/driver'!
Note that, although this file may be modified while experiments are running, lines
corresponding to completed or currently-running jobs should not be removed,
reordered, or otherwise displaced in this case. Such lines are identifiable by
the progress comments automatically added at the end of their lines.
tac
exit 1
fi
file="$1"
set -e
confirm() {
if [ $# -ne 2 ]
then
return 2
fi
local prompt="$1"
local default="$2"
set -e
case "$default" in
y)
local suffix=" (Y/n)"
;;
n)
local suffix=" (y/N)"
;;
*)
false
;;
esac
while true
do
printf %s "$prompt$suffix? "
read response
case "$response" in
"")
return "`[ "$default" = "y" ] && echo 0 || echo 1`"
;;
[yY])
return 0
;;
[nN])
return 1
;;
esac
done
}
oneline() {
if [ $# -lt 1 -o $# -gt 2 ]
then
return 1
fi
local linenum="$1"
local mode="-n"
local after=";p"
if [ $# -eq 2 ]
then
mode="-i"
after=""
if [ -n "$2" ]
then
after=";s/$/ # $2/"
fi
fi
set -e
sed "$mode" "$linenum{s/ \?#.*//$after}" "$file"
}
timestamp() {
date +"%F at %H:%M:%S %Z"
}
if [ ! -e "$file" ]
then
echo "$file: No such batch file" >&2
exit 2
fi
if grep "#" "$file" >/dev/null
then
cat <<-tac
+-----------------------------------------------------------------------------------------+
| |
| IMPORTANT: That batch queue already has commented lines, indicating that it may already |
| be running or have been run! Please take a moment to confirm your intent: |
| |
+-----------------------------------------------------------------------------------------+
tac
if confirm "Do you want to abort" y
then
exit 3
elif confirm "Do you want me to remove all comments from the file" n
then
sed -i 's/ \?#.*//' "$file"
fi
fi
cat <<-tac
------------------------
STARTING BATCH RUN FROM: $file
------------------------
tac
line="1"
while [ "$line" -le "`wc -l "$file" | cut -d" " -f1`" ]
do
args="`oneline "$line"`"
cat <<-tac
---------------------
STARTING BATCH JOB $line: ./driver $args
---------------------
tac
comment="launching"
for time in `seq 3`
do
comment="$comment."
oneline "$line" "$comment"
sleep 2
done
oneline "$line" "running since `timestamp`"
( trap - TERM && eval '"'"`dirname "$0"`/driver"'"' $args ) || true
oneline "$line" "completed at `timestamp`"
line="$((line + 1))"
done
cat <<-tac
------------------------
COMPLETED BATCH FUN RUN!
------------------------
tac