BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

service requirements
timfa Tim Falken · pushed by timfa · 6 months ago
7f110dab History
installsystemdservice.sh 1.2 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=${SCRIPT_DIR}/start.sh
36Restart=always
37RestartSec=5
38
39[Install]
40WantedBy=multi-user.target
41EOF
42
43echo "[install] Service file written to $SERVICE_FILE"
44
45systemctl daemon-reload
46systemctl enable "${SERVICE_NAME}"
47systemctl restart "${SERVICE_NAME}"
48
49echo "[install] Done. Service status:"
50systemctl status "${SERVICE_NAME}" --no-pager