BlueXIII's Blog

热爱技术,持续学习

0%

简易init.d脚本

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
#!/bin/sh

### BEGIN INIT INFO
# Provides: redsocks
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start redsocks daemon.
### END INIT INFO

SERVICE_NAME=redsocks
SERVICE_PATH=/usr/local/bin/redsocks
SERVICE_PARAM="-c /etc/redsocks.conf"
SERVICE_HOME=/home/pi
SERVICE_USER=pi
SERVICE_PID_FILE=/var/run/${SERVICE_NAME}.pid

start() {
start-stop-daemon --start \
--chdir "${SERVICE_HOME}" \
--chuid "${SERVICE_USER}" \
--user "${SERVICE_USER}" \
-b -m -p "${SERVICE_PID_FILE}" \
--exec ${SERVICE_PATH} -- ${SERVICE_PARAM}
}

stop() {
start-stop-daemon --stop \
--chdir "${SERVICE_HOME}" \
--chuid "${SERVICE_USER}" \
--user "${SERVICE_USER}" \
-p "${SERVICE_PID_FILE}"
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
# code to check status of app comes here
# example: status program_name
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0