-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-service
executable file
·68 lines (64 loc) · 1.97 KB
/
install-service
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
#!/bin/bash
PORT=8888
if [ $# -gt 0 ]; then PORT=$1; fi
PYTHON="$(which python3)"
if which realpath >/dev/null 2>&1; then
DIR="$(realpath -- "$(dirname -- "${BASH_SOURCE[0]}")")"
else
DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
fi
echo "Adding service to run:"
echo "'$PYTHON' '$DIR/server.py' --port $PORT >>'$DIR/output.log' 2>>'$DIR/error.log'"
if [ "$(uname -s)" == "Darwin" ]; then
echo "using launchd (macOS)"
SERVICE="/Library/LaunchDaemons/3d-print-server.plist"
sudo tee "$SERVICE" >/dev/null <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>edu.moravian.cslab.3d-printer-server</string>
<key>ServiceDescription</key>
<string>3D Printer Server</string>
<key>ProgramArguments</key>
<array>
<string>$PYTHON</string>
<string>$DIR/server.py</string>
<string>--port=$PORT</string>
</array>
<key>WorkingDirectory</key>
<string>$DIR</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>StandardOutPath</key>
<string>$DIR/output.log</string>
<key>StandardErrorPath</key>
<string>$DIR/error.log</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
sudo launchctl bootstrap system "$SERVICE"
else # assume Linux with systemd
echo "using systemd (Linux)"
SERVICE="/etc/systemd/system/3d-print-server.service"
sudo tee "$SERVICE" >/dev/null <<EOF
[Unit]
Description=3D Printer Server
Requires=network.target
[Service]
Type=exec
WorkingDirectory=$DIR
StandardOutput=append:$DIR/output.log
StandardError=append:$DIR/error.log
ExecStart="$PYTHON" "$DIR/server.py" --port=$PORT
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now 3d-print-server.service
fi