-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjobq.daemon
executable file
·339 lines (288 loc) · 9.49 KB
/
sjobq.daemon
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
########################################################################################
# Copyright (C) 2010-2017 by Nestor F. Aguirre #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, this #
# list of conditions and the following disclaimer. #
# 2. Redistributions in binary form must reproduce the above copyright notice, #
# this list of conditions and the following disclaimer in the documentation #
# and/or other materials provided with the distribution. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR #
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; #
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND #
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT #
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS #
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
########################################################################################
# This directory defines the identity of the daemon
DATA_DIR="/var/tmp/$USER/sjobq.data"
COUNTER_FILE="$DATA_DIR/counter"
STOP_FILE="$DATA_DIR/stop"
REFRESH_INTERVAL="2"
CURRENT_JOB_HOME="$DATA_DIR/current"
##
# @brief
##
diffTime()
{
local time0=`echo $1 | sed 's/@/ /g'`
local time1=`echo $2 | sed 's/@/ /g'`
local DAYS=""
local HOURS=""
local MINUTES=""
local SECONDS=""
# %F = full date, %T = %H:%M:%S, %N = nanoseconds, %Z = time zone.
DAYS=$(( $(printf '%s' $(( $(date -u -d"$time0" +%s) - $(date -u -d"$time1" +%s)))) / 60 / 60 / 24 ))
time1=$(date -d"$time1 +$DAYS days" '+%F %T.%N %Z')
HOURS=$(( $(printf '%s' $(( $(date -u -d"$time0" +%s) - $(date -u -d"$time1" +%s)))) / 60 / 60 ))
time1=$(date -d"$time1 +$HOURS hours" '+%F %T.%N %Z')
MINUTES=$(( $(printf '%s' $(( $(date -u -d"$time0" +%s) - $(date -u -d"$time1" +%s)))) / 60 ))
time1=$(date -d"$time1 +$MINUTES minutes" '+%F %T.%N %Z')
SECONDS=$(printf '%s' $(( $(date -u -d"$time0" +%s) - $(date -u -d"$time1" +%s))))
printf "%02d-%02d:%02d:%02d\n" "$DAYS" "$HOURS" "$MINUTES" "$SECONDS"
}
##
# @brief
##
function refreshQueue()
{
simultaneousJobs=`cat $DATA_DIR/sjobs`
for cid in `seq 1 $simultaneousJobs`
do
local lastID="`cat $CURRENT_JOB_HOME-$cid/pid 2> /dev/null`"
local currentPID=""
# Checks if the job is still running
if [ -z "`ps -u $USER | awk -v id=$lastID '($1==id){print "1"}'`" ]
then
pushd . &> /dev/null
cd $DATA_DIR
IDL="`ls *.bid 2> /dev/null | sed '{s/[.].*//g}' | sort -n | head -n1`"
popd &> /dev/null
# Saves the command to the history file
# exactly after this has finished
if [ "`cat $CURRENT_JOB_HOME-$cid/alive`" -eq "1" ]
then
beginTime=`date -u '+%F %T.%N %Z' | sed 's/ /@/g'`
endTime=$(cat $CURRENT_JOB_HOME-$cid/beginTime)
echo "command = `cat $CURRENT_JOB_HOME-$cid/com`" >> $DATA_DIR/history
echo "dir = `cat $CURRENT_JOB_HOME-$cid/pwd`" >> $DATA_DIR/history
echo "time spent = `diffTime $beginTime $endTime`" >> $DATA_DIR/history
echo "" >> $DATA_DIR/history
fi
# Flag that indicates the end of the job
echo "0" > $CURRENT_JOB_HOME-$cid/alive
# If the previous job has finished, it will run the next command in the queue
if [ -n "$IDL" ]
then
# Register the begin time
echo $(date -u '+%F %T.%N %Z' | sed 's/ /@/g') > $CURRENT_JOB_HOME-$cid/beginTime
# this will go to the appropriate directory
# where the command should be to executed
pushd . &> /dev/null
cd `cat $DATA_DIR/$IDL.pwd`
# this builds a litle script that run the command using nohup
# and updates the files necesary files for register it
randId=$RAND
echo "#!/bin/bash" > .tmp$randId
cat $DATA_DIR/$IDL.com | sed '{s/^/nohup /g;s/$/ \&/g}' >> .tmp$randId
echo "echo \$! > $CURRENT_JOB_HOME-$cid/pid" >> .tmp$randId
chmod +x .tmp$randId
bash ./.tmp$randId
rm .tmp$randId
rm -f $DATA_DIR/$IDL.bid
mv $DATA_DIR/$IDL.com $CURRENT_JOB_HOME-$cid/com
mv $DATA_DIR/$IDL.pwd $CURRENT_JOB_HOME-$cid/pwd
# this will return to the original directory
popd &> /dev/null
sleep 2
currentPID="`cat $CURRENT_JOB_HOME-$cid/pid`"
pstree -p $currentPID > $CURRENT_JOB_HOME-$cid/tree
pstree -p $currentPID | awk 'BEGIN{RS="[()]"}($0~/^[[:digit:]]+$/){printf $0" "}' | sed 's/$/\n/' > $CURRENT_JOB_HOME-$cid/pids
# Flag that indicate the begin of the job
echo "1" > $CURRENT_JOB_HOME-$cid/alive
fi
if [ -d "$CURRENT_JOB_HOME-$cid" ]
then
echo -n "Saving data for process $CURRENT_JOB_HOME-$cid ... "
else
echo -n "Making data directory for process in $CURRENT_JOB_HOME-$cid ... "
mkdir $CURRENT_JOB_HOME-$cid && echo "OK"
fi
fi
done
}
##
# @brief
##
function startDaemon()
{
while [ 1 ]
do
if [ -f $STOP_FILE ]
then
rm $STOP_FILE
exit
fi
sleep $REFRESH_INTERVAL
refreshQueue > /dev/null
done
}
##
# @brief
##
function stopDaemon()
{
echo "" > $STOP_FILE
sleep $REFRESH_INTERVAL
if [ "$1" = "clean" ]
then
simultaneousJobs=`cat $DATA_DIR/sjobs`
for cid in `seq 1 $simultaneousJobs`
do
if [ -f "$CURRENT_JOB_HOME-$cid/pids" ]
then
procIDList=`cat $CURRENT_JOB_HOME-$cid/pids`
procIDFirst=`echo $procIDList | awk '{print $1}'`
if [ -n "`ps -u $USER | awk '{print $1}' | grep "$procIDFirst"`" ]
then
kill -9 $procIDList
fi
echo "Job with id=current-$cid has been deleted !!"
fi
done
rm -rf $DATA_DIR
echo "The SjobQ configuration has been cleaned up successfully !!"
fi
}
##
# @brief
##
function pauseCurrentJob()
{
simultaneousJobs=`cat $DATA_DIR/sjobs`
for cid in `seq 1 $simultaneousJobs`
do
if [ -d "$CURRENT_JOB_HOME-$cid" ]
then
procIDList=`cat $CURRENT_JOB_HOME-$cid/pids`
procIDFirst=`echo $procIDList | awk '{print $1}'`
if [ -n "`ps -u $USER | awk '{print $1}' | grep "$procIDFirst"`" ]
then
kill -STOP $procIDList
fi
echo "Current-$cid job has been paused !!"
fi
done
}
##
# @brief
##
function continueCurrentJob()
{
simultaneousJobs=`cat $DATA_DIR/sjobs`
for cid in `seq 1 $simultaneousJobs`
do
if [ -d "$CURRENT_JOB_HOME-$cid" ]
then
procIDList=`cat $CURRENT_JOB_HOME-$cid/pids`
procIDFirst=`echo $procIDList | awk '{print $1}'`
if [ -n "`ps -u $USER | awk '{print $1}' | grep "$procIDFirst"`" ]
then
kill -CONT $procIDList
fi
echo "Current-$cid job has been continued !!"
fi
done
}
##
# @brief
##
function main()
{
case $1 in
start)
if [ -f "$DATA_DIR/daemon.pid" ]
then
runningIDs=`ps -u $USER | grep "sjobq.daemon$" | awk '{print $1}'`
for runningID in $runningIDs
do
if [ "$runningID" -eq "`cat $DATA_DIR/daemon.pid`" ]
then
echo "### Error ### The daemon sjobq.daemon is already running"
echo " You might want to run \"sjobq.daemon stop\" to stop it."
exit 1
fi
done
fi
if [ ! -d "$DATA_DIR" ]
then
mkdir -p $DATA_DIR
fi
nohup $0 __start > $DATA_DIR/log 2> $DATA_DIR/err &
simultaneousJobs=$2
[ -z "$simultaneousJobs" ] && simultaneousJobs=1
echo $simultaneousJobs > $DATA_DIR/sjobs
echo "==============================="
echo " SJobQ daemon has been started"
echo "==============================="
;;
stop)
runningIDs=`ps -u $USER | grep "sjobq.daemon$" | awk '{print $1}'`
loc=0
for runningID in $runningIDs
do
if [ -f "$DATA_DIR/daemon.pid" ]
then
if [ "$runningID" -eq "`cat $DATA_DIR/daemon.pid`" ]
then
stopDaemon $2
echo "==============================="
echo " SJobQ daemon has been stopped"
echo "==============================="
loc=1
fi
fi
done
if [ "$loc" -eq 0 ]
then
echo "### Error ### The daemon sjobq.daemon is not running"
echo " You might want to run \"sjobq.daemon start\" to correct this."
exit 1
fi
;;
pause)
pauseCurrentJob
;;
cont)
continueCurrentJob
;;
restart)
stopDaemon $2
if [ ! -d "$DATA_DIR" ]
then
mkdir -p $DATA_DIR
fi
nohup $0 __start > $DATA_DIR/log 2> $DATA_DIR/err &
echo "================================="
echo " SJobQ daemon has been restarted"
echo "================================="
;;
__start)
echo $$ > $DATA_DIR/daemon.pid
startDaemon
;;
*)
echo "Usage: sjobq.daemon {start|stop|restart}"
;;
esac
}
main $*