blob: 6cd0c3f9c122b7412538649791d1210201672762 (
plain) (
blame)
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
|
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Invalid parameter count: '$#'" >&2
exit 1
fi
BASE="$1"
shift
if [ -z "$BASE" ] || [ ! -r "$BASE" ] || [ ! -d "$BASE" ]; then
echo "Basedir invalid: '$BASE'" >&2
exit 2
fi
cd "$BASE"
[ "x$1" == "x--" ] && shift
# At this point $@ should only be ldadp instance ids
inArray () {
local e
for e in "${@:2}"; do
[[ "x$e" == "x$1" ]] && return 0
done
return 1
}
isInstance () {
if [ $# -ne 1 ]; then
echo "isInstance needs 1 param, got $#" >&2
return 0
fi
[ -L "/proc/$1/exe" ] && [ -r "/proc/$1/exe" ] && [[ "$(readlink -f "/proc/$1/exe")" == *ldadp ]] && return 0
return 1
}
launch () {
local CONFIG="${BASE}/configs/${1}.cfg"
if [ ! -r "$CONFIG" ]; then
echo "Told to start ldadp for module '${1}', but no config file found!" >&2
return 1
fi
echo "Launching #$1"
local LOGFILE="/var/log/ldadp/${1}.log"
if [ ! -w "/var/log/ldadp" ] || [ -e "$LOGFILE" -a ! -w "$LOGFILE" ]; then
LOGFILE="/dev/null"
elif [ -s "$LOGFILE" ]; then
TFILE=$(mktemp)
tail -n 50 "$LOGFILE" > "$TFILE"
echo "----- Starting $(date) -------" >> "$TFILE"
cat "$TFILE" > "$LOGFILE" # Use cat to preserve permissions of $LOGFILE
rm -f -- "$TFILE"
fi
"${BASE}/ldadp" -n "$CONFIG" >> "$LOGFILE" 2>&1 &
local P=$!
sleep 1
if ! kill -0 "$P" 2>/dev/null; then
echo "...FAILED to launch #$1" >&2
return 1
fi
echo -n "$P" > "${BASE}/pid/${1}"
return 0
}
RETVAL=0
for FILE in $(find "$BASE/pid" -type f -regextype posix-extended -regex "(^|.*/)[0-9]+$"); do
ID=$(basename $FILE)
PID=$(<$FILE)
if ! [[ "$PID" =~ ^[0-9]+$ ]]; then
echo "Invalid PID file: '$FILE'"
unlink "$FILE"
fi
inArray $ID "$@" && isInstance $PID && continue # Should be running, is running, nothing to do
#
unlink "$FILE"
if isInstance $PID; then
# Is running, should not, kill
kill $PID
sleep 1
isInstance $PID && kill -9 $PID
fi
done
while [ $# -gt 0 ]; do
ID=$1
shift
FILE="${BASE}/pid/${ID}"
[ -r "$FILE" ] && isInstance $(<$FILE) && continue
launch $ID
RET=$?
[ "$RET" != "0" ] && RETVAL=1
done
exit "$RETVAL"
|