-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjobq.client
executable file
·450 lines (389 loc) · 11.9 KB
/
sjobq.client
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/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"
CURRENT_JOB_HOME="$DATA_DIR/current"
MAX_HISTORY="10"
##
# @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 sjobq.stat()
{
pushd . &> /dev/null
cd $DATA_DIR
jobIDList=`ls *.bid 2> /dev/null | sed '{s/[.].*//g}' | sort -n`
popd &> /dev/null
lastIDAll=""
simultaneousJobs=`cat $DATA_DIR/sjobs`
for cid in `seq 1 $simultaneousJobs`
do
lastID="`cat $CURRENT_JOB_HOME-$cid/pid 2> /dev/null`"
lastIDAll="$lastIDAll-$lastID"
if [ -n "`ps -u $USER | awk -v id=$lastID '($1==id){print "1"}'`" ]
then
currentPID="`cat $CURRENT_JOB_HOME-$cid/pid`"
beginTime=`date -u '+%F %T.%N %Z' | sed 's/ /@/g'`
endTime=$(cat $CURRENT_JOB_HOME-$cid/beginTime)
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
echo "+-------------+"
echo "| Current job |"
echo "+-------------+"
echo ""
echo "cid = $cid"
echo "pid = `cat $CURRENT_JOB_HOME-$cid/pid`"
echo "command = `cat $CURRENT_JOB_HOME-$cid/com`"
echo "dir = `cat $CURRENT_JOB_HOME-$cid/pwd | sed 's/^.*\/home\/'$USER'\//~\//g'`"
echo "time spent = `diffTime $beginTime $endTime`"
echo "pids = `cat $CURRENT_JOB_HOME-$cid/pids`"
echo "tree ="
cat $CURRENT_JOB_HOME-$cid/tree | sed '{s/^/\t/g}'
echo ""
fi
done
if [ -z "$jobIDList" -a -z "$lastIDAll" ]
then
echo "Empty queue! You might want to run \"sjobq.push\" first."
exit
fi
if [ -n "$jobIDList" ]
then
maxCharInDirColumn=0
for procID in $jobIDList
do
nCharInDir=`cat $DATA_DIR/$procID.pwd | sed 's/^.*\/home\/'$USER'\//~\//g' | wc -c`
if [ "$maxCharInDirColumn" -lt "$nCharInDir" ]; then maxCharInDirColumn=$nCharInDir; fi
done
if [ "$maxCharInDirColumn" -lt 20 ]; then maxCharInDirColumn=20; fi
rowSeparator=`echo -n "+-------+"`
rowSeparator=$rowSeparator`echo "-" | awk '{for(i=0;i<='$maxCharInDirColumn';i++) printf($1)}'`
rowSeparator=$rowSeparator`echo "+--------------------"`
printf "%s\n" "+-------+"
echo "| QUEUE |"
echo $rowSeparator
printf "|%6s |%"$maxCharInDirColumn"s | %s\n" "id" "directory" "command"
printf "|%6s |%"$maxCharInDirColumn"s | %s\n" "" "" ""
for procID in $jobIDList
do
id=`cat $DATA_DIR/$procID.bid`
dir=`cat $DATA_DIR/$procID.pwd | sed 's/^.*\/home\/'$USER'\//~\//g'`
command=`cat $DATA_DIR/$procID.com`
printf "|%6s |%"$maxCharInDirColumn"s | " $id $dir
echo $command
done
echo $rowSeparator
fi
echo ""
if [ -f "$DATA_DIR/history" ]
then
maxCharInDirColumn=`tail -n $(( 4*$MAX_HISTORY )) $DATA_DIR/history \
| grep -E "dir\s+=" \
| sed -E 's/\s+\/.*\/home\/'$USER'\// ~\//g' \
| sed -r 's/dir\s+=//g' \
| gawk '(length(line)<length($0)){line=$0}END{print line}' | wc -c`
rowSeparator=`echo -n "+-------------+"`
rowSeparator=$rowSeparator`echo "-" | awk '{for(i=0;i<'$maxCharInDirColumn';i++) printf($1)}'`
rowSeparator=$rowSeparator`echo "+--------------------"`
printf "%s\n" "+-------------+"
echo "| HISTORY |"
echo $rowSeparator
printf "|%12s |%"$maxCharInDirColumn"s| %s\n" "time spent" "directory " "command"
printf "|%12s |%"$maxCharInDirColumn"s| %s\n" "" "" ""
tail -n $(( 4*$MAX_HISTORY )) $DATA_DIR/history | sed -E 's/\s+\/.*\/home\/'$USER'\// ~\//g' | \
gawk '
BEGIN{
n=0
}
($1=="time"){
timeSpent = sprintf("%12s",$4)
line[n] = "|"timeSpent" |"dir" | "command
n+=1
}
($1=="dir"){
dir = sprintf("%'$(($maxCharInDirColumn-1))'s",$3)
}
($1=="command"){
gsub("command[[:blank:]]+=[[:blank:]]+", "")
command = sprintf("%s",$0)
}
END{
for(i=0;i<n;i++)
print line[i]
}'
echo $rowSeparator
fi
}
##
# @brief
##
function deleteJob()
{
local jobID=$1
pushd . &> /dev/null
cd $DATA_DIR
jobIDList=`ls *.bid 2> /dev/null | sed '{s/[.].*//g}'`
popd &> /dev/null
if [ $jobID = "current" ]
then
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 -9 $procIDList
fi
echo "Job with id=current-$cid has been deleted !!"
fi
done
return
fi
if [[ $jobID =~ current-* ]]
then
cid=`echo $jobID | sed 's/current-//g'`
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 -9 $procIDList
fi
echo "Job with id=current-$cid has been deleted !!"
else
echo "Job with id=$jobID not found !!"
fi
return
fi
exist="0"
for IDL in $jobIDList
do
if [ $IDL -eq $jobID ]
then
exist="1"
rm $DATA_DIR/$jobID.bid
rm $DATA_DIR/$jobID.com
rm $DATA_DIR/$jobID.pwd
echo "Job with id=$jobID has been deleted !!"
return
fi
done
if [ $exist -eq "0" ]
then
echo "Job with id=$jobID not found !!"
fi
}
##
# @brief
##
function sjobq.push()
{
local command=$*
if [ -z "$command" ]
then
echo "### ERROR ### Empty job. The queue will not be affected."
exit
fi
local jobID="X"
if [ -f "$COUNTER_FILE" ]
then
previous=`cat $COUNTER_FILE`
if [ $previous -eq "32767" ]
then
jobID="1"
else
jobID=$(( $previous + 1 ))
fi
else
echo "1" > $COUNTER_FILE
jobID=`cat $COUNTER_FILE`
fi
echo $jobID > $COUNTER_FILE
echo "id = $jobID"
echo "command = $command"
echo "dir = `echo $PWD | sed 's/^.*\/home\/'$USER'\//~\//g'`"
echo "$jobID" > $DATA_DIR/$jobID.bid
echo "$command" > $DATA_DIR/$jobID.com
echo "$PWD" > $DATA_DIR/$jobID.pwd
}
##
# @brief
##
function sjobq.sort()
{
local action=$1
local jobID=$2
local exchangeID=$3
cd $DATA_DIR
local firstID=`ls -l *.bid 2> /dev/null | awk '{print $NF}' | sed '{s/[.].*//g}' | sort -n | head -n1`
local lastID=`ls -l *.bid 2> /dev/null | awk '{print $NF}' | sed '{s/[.].*//g}' | sort -n | tail -n1`
local beforeID=`ls -l *.bid 2> /dev/null | awk '{print $NF}' | sed '{s/[.].*//g}' | sort -n | grep $jobID -B1 2> /dev/null | head -n1`
local afterID=`ls -l *.bid 2> /dev/null | awk '{print $NF}' | sed '{s/[.].*//g}' | sort -n | grep $jobID -A1 2> /dev/null | tail -n1`
popd &> /dev/null
if [ -z "$firstID" ]
then
echo "### ERROR ### Impossible to locate the ID for the first job"
exit
fi
if [ -z "$lastID" ]
then
echo "### ERROR ### Impossible to locate the ID for the last job"
exit
fi
case $action in
"raise")
targetID=$beforeID
;;
"lower")
targetID=$afterID
;;
"top")
targetID=$firstID
;;
"bottom")
targetID=$lastID
;;
"swap")
if [ -n "$exchangeID" ]
then
targetID=$exchangeID
else
echo "### ERROR ### sjobq.sort swap jobID jobID2"
echo " ^ "
echo " Target ID (jobID2) undefined"
exit
fi
;;
*)
echo "Usage: sjobq.sort command jobID [jobID2]"
echo ""
echo "Available commands:"
echo " - raise"
echo " Raises the specified job into the queue"
echo " - lower"
echo " Lowers the specified job into the queue"
echo " - top"
echo " Moves the specified jobID to the top of the queue"
echo " - bottom"
echo " Moves the specified jobID to the bottom of the queue"
echo " - swap"
echo " Swaps the positions of the specified jobs jobID and jobID2 in the queue"
echo ""
exit
esac
if [ "$jobID" -ne "$targetID" ]
then
cd $DATA_DIR
mv $jobID.com tmp$$.com
mv $jobID.pwd tmp$$.pwd
mv $targetID.com $jobID.com
mv $targetID.pwd $jobID.pwd
mv tmp$$.com $targetID.com
mv tmp$$.pwd $targetID.pwd
popd &> /dev/null
fi
}
##
# @brief
##
function sjobq.delete()
{
if [ -n "$*" ]
then
for delId in $*
do
deleteJob $delId
done
else
echo "Usage: sjobq.del jobID"
exit
fi
}
##
# @brief
##
function main()
{
local command=$1
shift # $1 will be discarded
if [ -z "`ps -u $USER | grep "sjobq.daemon$"`" ]
then
echo "### Error ### The daemon sjobq.daemon is not running"
echo " You might want to run \"sjobq.daemon start\" to fix this."
exit 1
fi
if [ ! -d "$DATA_DIR" ]
then
echo "### Error ### Data directory $DATA_DIR is empty"
echo " You might want to run \"sjobq.daemon restart\" to fix this."
exit 1
fi
case $command in
"stat")
sjobq.stat $*
;;
"push")
sjobq.push $*
;;
"sort")
sjobq.sort $*
;;
"delete")
sjobq.delete $*
;;
*)
echo "### ERROR ### Unknown command"
echo " You might want to run upper the level commands:"
echo " sjobq.stat, sjobq.push, sjobq.sort, sjobq.del"
exit
esac
}
main $*