-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_systemd.sh
More file actions
executable file
·101 lines (84 loc) · 2.27 KB
/
setup_systemd.sh
File metadata and controls
executable file
·101 lines (84 loc) · 2.27 KB
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
#!/usr/bin/env bash
set -euo pipefail
SERVICE_NAME="intra_backend.service"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$SCRIPT_DIR/backend"
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME"
RUN_USER="${SUDO_USER:-$(whoami)}"
usage() {
cat <<USAGE
Usage:
sudo ./setup_systemd.sh install # install + enable + start service
sudo ./setup_systemd.sh uninstall # stop + disable + remove service
USAGE
}
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "❌ Please run as root: sudo ./setup_systemd.sh <install|uninstall>"
exit 1
fi
}
install_service() {
if [ ! -d "$BACKEND_DIR" ]; then
echo "❌ Error: backend folder not found at $BACKEND_DIR"
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "❌ python3 not found. Install Python 3 first."
exit 1
fi
if [ ! -d "$BACKEND_DIR/venv" ]; then
echo "📦 Creating virtual environment..."
python3 -m venv "$BACKEND_DIR/venv"
fi
echo "🔄 Installing dependencies..."
"$BACKEND_DIR/venv/bin/python" -m pip install --upgrade pip
"$BACKEND_DIR/venv/bin/python" -m pip install -r "$BACKEND_DIR/requirements.txt"
cat > "$SERVICE_FILE" <<EOF_SERVICE
[Unit]
Description=Intra Backend Server Service
After=network.target
[Service]
Type=simple
User=$RUN_USER
WorkingDirectory=$BACKEND_DIR
ExecStart=$BACKEND_DIR/venv/bin/python $BACKEND_DIR/run_server.py --host 0.0.0.0 --port 8000 --no-reload
Restart=always
RestartSec=3
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF_SERVICE
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
echo "✅ Service installed and running."
echo "Status: sudo systemctl status $SERVICE_NAME"
}
uninstall_service() {
systemctl stop "$SERVICE_NAME" 2>/dev/null || true
systemctl disable "$SERVICE_NAME" 2>/dev/null || true
if [ -f "$SERVICE_FILE" ]; then
rm -f "$SERVICE_FILE"
echo "🗑️ Removed $SERVICE_FILE"
else
echo "ℹ️ Service file not found, skipping remove"
fi
systemctl daemon-reload
systemctl reset-failed
echo "✅ Service uninstalled."
}
ACTION="${1:-install}"
check_root
case "$ACTION" in
install)
install_service
;;
uninstall|remove)
uninstall_service
;;
*)
usage
exit 1
;;
esac