-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_install.sh
executable file
·184 lines (153 loc) · 4 KB
/
service_install.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
readonly PYTHON_PATH=/usr/bin/python
readonly SERVICE_NAME=cloud4rpi
quit_on_error() {
test "0" = $? || {
exit 1
}
}
put_systemd_script(){
local INIT_MODULE_PATH=/lib/systemd/system/$SERVICE_NAME.service
echo "Writing init script to $INIT_MODULE_PATH..."
cat > "$INIT_MODULE_PATH" <<EOF
[Unit]
Description=Cloud4RPi-enabled user script
After=network.target
[Service]
Type=idle
ExecStart=$PYTHON_PATH $SCRIPT_PATH
[Install]
WantedBy=multi-user.target
EOF
quit_on_error
echo "Setting permissions..."
chmod 644 "$INIT_MODULE_PATH"
quit_on_error
}
put_systemv_script(){
local INIT_MODULE_PATH=/etc/init.d/$SERVICE_NAME
echo "Writing init script to $INIT_MODULE_PATH..."
cat > "$INIT_MODULE_PATH" <<EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: $SERVICE_NAME
# 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
# Short-Description: Cloud4RPi daemon
# Description: Cloud4RPi-enabled user script
# (https://cloud4rpi.io/)
### END INIT INFO
SCRIPT=$SCRIPT_PATH
RUNAS=root
PIDFILE=/var/run/$SERVICE_NAME.pid
LOGFILE=/var/log/$SERVICE_NAME.log
start() {
if is_running; then
echo 'Service is already running' >&2
exit 1
fi
echo 'Starting service...' >&2
# Clears the old log
echo '--- Service started at' "\$(date)" '---' > "\$LOGFILE"
sudo -u \$RUNAS $PYTHON_PATH -u "\$SCRIPT" >> \$LOGFILE 2>>\$LOGFILE &
PID=\$!
ERROR_LEVEL=0
if [ -z \$PID ]; then
echo 'Failed to run.' >&2
ERROR_LEVEL=1
else
echo \$PID > "\$PIDFILE"
echo 'Service started.' >&2
fi
echo 'See the log for details:' \$LOGFILE >&2
exit \$ERROR_LEVEL
}
stop() {
if ! is_running; then
echo 'Service not running' >&2
return 0
fi
echo 'Stopping service...' >&2
if ! kill \$(cat "\$PIDFILE"); then
echo 'Failed to stop.' >&2
exit 1
fi
rm -f "\$PIDFILE"
echo 'Service stopped' >&2
echo '--- Service stopped at' "\$(date)" '---' >> "\$LOGFILE"
}
uninstall() {
printf "Do you really want to uninstall this Cloud4RPi service? That cannot be undone. [yes|no] "
read -r REPLY
if [ "\$REPLY" = "yes" ]; then
stop
echo "Notice: log file was not removed: '\$LOGFILE'" >&2
update-rc.d -f $SERVICE_NAME remove
rm -fv "\$0"
fi
}
is_running() {
[ -f "\$PIDFILE" ] && ps \$(cat "\$PIDFILE") > /dev/null 2>&1
}
case "\$1" in
start) start ;;
stop) stop ;;
uninstall) uninstall ;;
restart) stop; start ;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: \$0 {start|stop|status|restart|uninstall}"
esac
EOF
quit_on_error
echo "Setting permissions..."
chmod 755 "$INIT_MODULE_PATH"
quit_on_error
}
install_sysv() {
put_systemv_script
echo "Installing init script links..."
update-rc.d "$SERVICE_NAME" defaults
quit_on_error
echo "All done!"
echo "Usage example:"
echo -e " $ sudo service \e[1m$SERVICE_NAME\e[0m start|stop|status|restart|uninstall"
}
install_sysd() {
put_systemd_script
echo "Configuring systemd..."
systemctl daemon-reload
systemctl enable "$SERVICE_NAME.service"
quit_on_error
echo "All done!"
echo "Usage example:"
echo -e " $ sudo systemctl start|stop|status \e[0m$SERVICE_NAME.service\e[1m"
}
main() {
SCRIPT_PATH=$(readlink -f "$1")
if [ ! -f "$SCRIPT_PATH" ]; then
echo "Usage: sudo path/to/service_install.sh path/to/target_script.py"
echo "Invalid script path. Make sure it exists."
exit 1
fi
chmod +x "$SCRIPT_PATH"
quit_on_error
case $(ps -p 1 -o comm=) in
"init") install_sysv ;;
"systemd") install_sysd ;;
*)
echo "Unfortunately we can\'t automate service installation on your system. Please contact us for help: https://cloud4rpi.answerdesk.io/"
exit 1
esac
exit 0
}
main "$1"