-
Notifications
You must be signed in to change notification settings - Fork 4
/
watchdog.sh
executable file
·69 lines (63 loc) · 2.04 KB
/
watchdog.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
67
68
69
#!/bin/bash
SCRIPT_FOLDER=$(cd $(dirname $(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")) && pwd -P)
source "$SCRIPT_FOLDER/core_lib/includeBasics.sh"
SLEEP_TIME="60s"
# capture STRG+C signals
trap 'handle_int' SIGINT
# redirect stdin status
REDIRECT_STDIN=0
# auto-configure examples if environment variable WATCHDOG_HOME is set and WATCHDOG_AUTO_CONFIG is set to 1
if [ "$WATCHDOG_AUTO_CONFIG" == "1" ] && [ ! -z "$WATCHDOG_HOME" ]; then
if [ -d "$WATCHDOG_HOME/examples" ] && [ ! -f "$WATCHDOG_HOME/examples/example_basic_sleep.xml" ]; then
(bash -c "$WATCHDOG_HOME/helper_scripts/configureExamples.sh -i '$WATCHDOG_HOME' 2>&1 > /dev/null") 2>&1 > /dev/null
fi
fi
# check if watchdog should run in start&stop mode
AUTO_DETACH_COUNT=$(echo "$@" | grep -c "\-autoDetach")
ATTACH_INFO_COUNT=$(echo "$@" | grep -c "\-attachInfo")
if [ $AUTO_DETACH_COUNT -eq 1 ]; then
# generate tmp location for status information
if [ $ATTACH_INFO_COUNT -eq 0 ]; then
ATTACH_FILE=$(getTmpFile watchdog.attach)
APPEND_PARAM="-attachInfo \"$ATTACH_FILE\""
# ensure that file is deleted on exit
deleteFileOnExit $ATTACH_FILE
fi
RET=123
while [ $RET -eq 123 ]; do
set -m
startRedirectStdin
if [ $REDIRECT_STDIN -eq 1 ]; then
java -jar $SCRIPT_FOLDER/jars/watchdog.jar $@ $APPEND_PARAM <&3 &
else
java -jar $SCRIPT_FOLDER/jars/watchdog.jar $@ $APPEND_PARAM &
fi
BACKGROUND_PID=$!
set +m
waitForCommandToFinish ${BACKGROUND_PID}
endRedirectStdin
RET=${BACKGROUND_EXIT_CODE}
if [ $RET -eq 123 ]; then
# wait some time until watchdog is re-attached
echo "------------------"
echo "sleeping for $SLEEP_TIME"
echo "------------------"
sleep "$SLEEP_TIME"
fi
done
exit $RET
else
set -m
startRedirectStdin
if [ $REDIRECT_STDIN -eq 1 ]; then
java -jar "$SCRIPT_FOLDER/jars/watchdog.jar" $@ <&3 &
else
java -jar "$SCRIPT_FOLDER/jars/watchdog.jar" $@ &
fi
BACKGROUND_PID=$!
set +m
waitForCommandToFinish ${BACKGROUND_PID}
endRedirectStdin
exit $BACKGROUND_EXIT_CODE
fi
exit $?