forked from zonyl/pytomation
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pytomation.init
executable file
·116 lines (102 loc) · 2.83 KB
/
pytomation.init
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
107
108
109
110
111
112
113
114
115
#!/bin/sh
### BEGIN INIT INFO
# Provides: pytomation
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Pytomation Home Automation Server
### END INIT INFO
# Service Description
NAME="pytomation"
DESC="Pytomation Home Automation Server"
SCRIPT_NAME="/etc/init.d/$NAME"
# Python interpreter and Pytomation script location
INTERPRETER="/usr/bin/python3"
PYTOMATION_DIR="/home/pytomation"
PYTOMATION_SCRIPT="$PYTOMATION_DIR/pytomation.py"
# User for Pytomation daemon
PYTOMATION_USER="pyto"
# PID file name and location
PID_FILE="/var/run/$NAME.pid"
# Fail gracefully if package has been removed
[ -x "$INTERPRETER" ] || exit 0
[ -x "$PYTOMATION_SCRIPT" ] || exit 0
# Start with a reasonable PATH setting
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Load rcS variables
. /lib/init/vars.sh
# Load standard LSB functions including log_* functions.
# Requires lsb-base (>= 3.0-6)
. /lib/lsb/init-functions
# Start Pytomation
do_start()
{
start-stop-daemon --start \
--quiet \
--background \
--pidfile "$PID_FILE" \
--make-pidfile \
--chdir "$PYTOMATION_DIR" \
--chuid "$PYTOMATION_USER" \
--exec "$INTERPRETER" -- $PYTOMATION_SCRIPT \
|| return 1
}
# Stop Pytomation
do_stop()
{
local result
start-stop-daemon --stop \
--quiet \
--pidfile "$PID_FILE"
result="$?"
[ "$result" = 2 ] && return 2
if [ -e "$PIDFILE" ]; then
rm "$PID_FILE"
fi
return $result
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
status)
status_of_proc -p "$PID_FILE" "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;; # Success
1) log_end_msg 1 ;; # Old process still running
*) log_end_msg 1 ;; # Could not start new process
esac
;;
*)
# Could not stop process
log_end_msg 1
;;
esac
;;
*) # Display a usage option.
echo "Usage: $SCRIPT_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0