-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.sh
49 lines (42 loc) · 965 Bytes
/
service.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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Keep the pwd in mind!
# Example: RUN="java -jar $DIR/target/magic.jar"
RUN="java -jar $DIR/target/bus-route-challenge.jar"
NAME=bus-route-challenge
DATA_FILE=$2
PIDFILE=/tmp/$NAME.pid
LOGFILE=/tmp/$NAME.log
start() {
if [ -f $PIDFILE ]; then
if kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
else
rm -f $PIDFILE
fi
fi
local CMD="$RUN $DATA_FILE &> \"$LOGFILE\" & echo \$!"
sh -c "$CMD" > $PIDFILE
}
stop() {
if [ ! -f $PIDFILE ] || ! kill -0 $(cat $PIDFILE); then
echo 'Service not running' >&2
return 1
fi
kill -15 $(cat $PIDFILE) && rm -f $PIDFILE
}
case $1 in
start)
start
;;
stop)
stop
;;
block)
start
sleep infinity
;;
*)
echo "Usage: $0 {start|stop|block} DATA_FILE"
esac