-
Notifications
You must be signed in to change notification settings - Fork 2
/
plowdown_parallel.sh
executable file
·66 lines (60 loc) · 1.42 KB
/
plowdown_parallel.sh
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
#/bin/bash
#
# Launch parallel plowdown processes for different websites
#
set -e
debug() { echo "$@" >&2; }
groupby() {
local PREDICATE=$1
local RETURN_FUNC=$2
local LAST=
local FIRST=1
while read LINE; do
local VALUE=$(echo $LINE | eval $PREDICATE)
local RETURN=$(echo $LINE | eval $RETURN_FUNC)
if test "$FIRST" = "1"; then
echo -n "$VALUE: $RETURN"
FIRST=0
elif test "$LAST" = "$VALUE"; then
echo -n " $RETURN"
else
echo
echo -n "$VALUE: $RETURN"
fi
LAST=$VALUE
done
echo
}
cleanup() {
ps awx > a.a
local PIDS=($(ps x -o "%p %r" | awk "\$1 != $$ && \$2 == $$" |
awk '{print $1}' | xargs))
debug "cleanup: pids ${PIDS[*]}"
for PID in ${PIDS[*]}; do
kill -0 $PID 2>/dev/null && kill -TERM $PID
done
sleep 2
for PID in ${PIDS[*]}; do
kill -0 $PID 2>/dev/null && kill -9 $PID 2>/dev/null
done
debug "cleanup: done"
}
INFILE=$1
PIDS=()
while read MODULE URLS; do
debug "Run: plowdown $URLS"
plowdown $URLS &
PID=$!
PIDS[$PID]=$PID
done < <(cat $INFILE | while read URL; do
MODULE=$(plowdown --get-module $URL)
echo "$MODULE $URL"
done | sort -k1 | groupby "cut -d' ' -f1" "cut -d' ' -f2")
trap cleanup SIGINT SIGTERM
while test ${#PIDS[*]} -ne 0; do
#debug "Waiting for pids: ${PIDS[*]}"
for PID in ${PIDS[*]}; do
kill -0 $PID 2>/dev/null || { wait $PID && unset PIDS[$PID]; }
done
sleep 1
done