BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

block mobile
timfa Tim Falken · pushed by timfa · 6 months ago
4a4ba9a7 History
installsystemdservice.sh 1.4 KiB Download Raw
1#!/usr/bin/env bash
2# installsystemdservice.sh — Installs filemanager as a systemd service.
3# Must be run as root: sudo ./installsystemdservice.sh
4
5set -euo pipefail
6
7if [[ "$(id -u)" -ne 0 ]]; then
8 echo "Error: this script must be run as root (use sudo)." >&2
9 exit 1
10fi
11
12SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13SERVICE_NAME="filemanager"
14SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
15
16# Resolve the user who owns the project directory (i.e. the user who cloned it,
17# not root). Falls back to the SUDO_USER env var, then 'pi'.
18RUN_AS="${SUDO_USER:-pi}"
19
20echo "[install] Project directory : $SCRIPT_DIR"
21echo "[install] Service will run as user: $RUN_AS"
22
23# Make sure start.sh is executable
24chmod +x "$SCRIPT_DIR/start.sh"
25
26cat > "$SERVICE_FILE" <<EOF
27[Unit]
28Description=File Manager
29After=network.target
30
31[Service]
32Type=simple
33User=${RUN_AS}
34WorkingDirectory=${SCRIPT_DIR}
35ExecStart=/bin/sh ${SCRIPT_DIR}/start.sh
36Restart=always
37RestartSec=5
38# Allow binding to privileged ports (< 1024) without running as root.
39AmbientCapabilities=CAP_NET_BIND_SERVICE
40CapabilityBoundingSet=CAP_NET_BIND_SERVICE
41
42[Install]
43WantedBy=multi-user.target
44EOF
45
46echo "[install] Service file written to $SERVICE_FILE"
47
48systemctl daemon-reload
49systemctl enable "${SERVICE_NAME}"
50systemctl restart "${SERVICE_NAME}"
51
52echo "[install] Done. Service status:"
53systemctl status "${SERVICE_NAME}" --no-pager