-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki-runjobs
106 lines (97 loc) · 2.35 KB
/
wiki-runjobs
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
#!/bin/sh
### BEGIN INIT INFO
# Provides: wiki-runjobs
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Executes runjobs for a single instance or a farm, depending on config
### END INIT INFO
SCRIPT=<COMMAND>
NAME=parallel-runjobs-service
PIDFILE=/var/run/wiki-runjobs.pid
LOG_FILE="/var/log/wiki-runjobs.log"
MAX_LOG_SIZE=10 # in MB
ROTATION_INTERVAL=600 # in seconds
start() {
if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT >> $LOG_FILE 2>&1 & echo \$!"
sh -c "$CMD" > "$PIDFILE"
rotate_logs &
sleep 2
PID=$(cat $PIDFILE)
if pgrep -f $NAME > /dev/null
then
echo "$NAME is now running, the PID is $PID"
else
echo ''
echo "Error! Could not start $NAME!"
fi
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
update-rc.d -f $NAME remove
rm -fv "$0"
else
echo "Abort!"
fi
}
status() {
printf "%-50s" "Checking farm-runjobs..."
if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
rotate_logs() {
while true; do
if [ -f "$LOG_FILE" ] && [ "$(du -k "$LOG_FILE" | cut -f1)" -gt "$MAX_LOG_SIZE" ]; then
> "$LOG_FILE"
fi
sleep "$ROTATION_INTERVAL"
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac