Skip to content

Commit 4f8e9d1

Browse files
build(pkg:linux): sysv-style init script support
Signed-off-by: Ar Rakin <[email protected]>
1 parent 4de34e8 commit 4f8e9d1

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

packages/common/postinst

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ if [ ! -d /var/sudobot ]; then
1717
mkdir -p /var/sudobot
1818
fi
1919

20+
# Create directories
21+
if [ ! -d /var/log/sudobot ]; then
22+
mkdir -p /var/log/sudobot
23+
fi
24+
25+
touch /var/run/sudobot.pid
26+
27+
chown sudobot:sudobot /var/run/sudobot.pid
28+
chmod 644 /var/run/sudobot.pid
29+
30+
chown -R sudobot:sudobot /var/log/sudobot
2031
chown -R sudobot:sudobot /var/sudobot
2132
chown -R sudobot:sudobot /usr/share/sudobot
2233
chown -R sudobot:sudobot /etc/sudobot
@@ -57,9 +68,101 @@ su sudobot -c "PATH=\"$PATH\" BUN_INSTALL=/var/sudobot $pm install" -s /bin/sh |
5768
}
5869

5970
# Enable systemd service
60-
if [ -f /lib/systemd/system/sudobot.service ]; then
71+
if [ -f /lib/systemd/system/sudobot.service ] && command -v systemctl >/dev/null 2>&1; then
6172
if command -v deb-systemd-helper >/dev/null 2>&1; then
6273
deb-systemd-helper enable sudobot.service || true
6374
fi
6475
fi
6576

77+
# Create SystemV init script
78+
if command -v update-rc.d >/dev/null 2>&1; then
79+
cat << 'EOF' > /etc/init.d/sudobot
80+
#!/bin/sh
81+
### BEGIN INIT INFO
82+
# Provides: sudobot
83+
# Required-Start: $local_fs $network
84+
# Required-Stop: $local_fs $network
85+
# Default-Start: 2 3 4 5
86+
# Default-Stop: 0 1 6
87+
# Short-Description: Start sudobot daemon
88+
# Description: Start the sudobot daemon
89+
### END INIT INFO
90+
91+
LOG_FILE="/var/log/sudobot/sudobot.log"
92+
PID_FILE="/var/run/sudobot.pid"
93+
94+
start() {
95+
nohup su -s /bin/sh -c "/usr/bin/sudobot" >> "$LOG_FILE" 2>&1 &
96+
97+
if [ ! -f "$PID_FILE" ]; then
98+
touch "$PID_FILE"
99+
chown sudobot:sudobot "$PID_FILE"
100+
chmod 644 "$PID_FILE"
101+
fi
102+
103+
echo $! > "$PID_FILE"
104+
}
105+
106+
stop() {
107+
if [ ! -f "$PID_FILE" ]; then
108+
return 1
109+
fi
110+
111+
PID="$(cat "$PID_FILE" 2>/dev/null)"
112+
113+
if [ -z "$PID" ]; then
114+
rm -f "$PID_FILE"
115+
return 1
116+
fi
117+
118+
if kill -0 "$PID" >/dev/null 2>&1; then
119+
kill "$PID"
120+
rm -f "$PID_FILE"
121+
else
122+
rm -f "$PID_FILE"
123+
return 1
124+
fi
125+
}
126+
127+
case "$1" in
128+
start)
129+
echo "Starting sudobot..."
130+
start
131+
;;
132+
stop)
133+
echo "Stopping sudobot..."
134+
stop
135+
136+
if [ $? -ne 0 ]; then
137+
echo "sudobot is not running or could not be stopped."
138+
exit $?
139+
fi
140+
;;
141+
restart)
142+
echo "Restarting sudobot..."
143+
stop
144+
start
145+
;;
146+
status)
147+
if [ -f "$PID_FILE" ]; then
148+
PID="$(cat "$PID_FILE" 2>/dev/null)"
149+
150+
if [ -n "$PID" ] && kill -0 "$PID" >/dev/null 2>&1; then
151+
echo "sudobot is running with PID $PID."
152+
else
153+
echo "sudobot is not running."
154+
fi
155+
else
156+
echo "sudobot is not running."
157+
fi
158+
;;
159+
*)
160+
echo "Usage: $0 {start|stop|restart|status}"
161+
exit 1
162+
;;
163+
esac
164+
exit 0
165+
EOF
166+
167+
update-rc.d sudobot defaults || true
168+
fi

packages/common/prerm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ fi
2222

2323
if getent group sudobot >/dev/null 2>&1; then
2424
groupdel sudobot
25-
fi
25+
fi
26+
27+
rm /var/run/sudobot.pid

0 commit comments

Comments
 (0)