-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.sh
81 lines (80 loc) · 1.87 KB
/
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
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
#!/bin/bash
ARG_1="$1"
OPT_RESTART=n
OPT_RUNNING=n
OPT_RESUME_AFTER_SETUP=n
detect_if_running() {
OPT_RUNNING=n
if [ -f "server.lock" ] && [ -f "server.pid" ]; then
pid=$(head -1 server.pid)
OSTYPE=$(uname | tr '[:upper:]' '[:lower:]')
if [[ "$OSTYPE" == "darwin"* ]] || [[ "$OSTYPE" == "freebsd"* ]]; then
cmd_name=$(ps -p "$pid" -o comm)
if [[ "$cmd_name" == *"/java" ]]; then
cmd_name=java
fi
else
cmd_name=$(head -1 "/proc/$pid/comm")
fi
if [ "$cmd_name" == "java" ]; then
OPT_RUNNING=y
fi
fi
}
start() {
detect_if_running
if [ "$OPT_RUNNING" == "y" ]; then
echo Service already running.
else
(java -server -cp bin com.jcope.vnc.Server server.properties 2>&1 | tee server.log 2>&1) &
echo Service started.
fi
}
stop() {
detect_if_running
if [ "$OPT_RUNNING" == "n" ]; then
if [ "$OPT_RESTART" == "y" ]; then
start
else
echo Service already terminated.
fi
else
pid=$(head -1 server.pid)
kill -9 "$pid"
rm "server.lock" >/dev/null 2>&1
rm "server.pid" >/dev/null 2>&1
echo Service terminated.
if [ "$OPT_RESTART" == "y" ]; then
start
fi
fi
}
restart() {
OPT_RESTART=y
stop
}
unknown() {
echo Unknown command "\"$ARG_1\""
}
setup() {
detect_if_running
OPT_RESUME_AFTER_SETUP="$OPT_RUNNING"
if [ "$OPT_RESUME_AFTER_SETUP" == "y" ]; then
stop
fi
(java -client -cp bin com.jcope.vnc.ServerSetup server.properties 2>&1 | tee setup.log 2>&1)
if [ "$OPT_RESUME_AFTER_SETUP" == "y" ]; then
start
fi
}
if [ "$ARG_1" == "start" ]; then
start
elif [ "$ARG_1" == "stop" ]; then
stop
elif [ "$ARG_1" == "restart" ]; then
restart
elif [ "$ARG_1" == "setup" ]; then
setup
else
unknown
fi