Files
ScheduleTaskManager/agent/linux/install.sh
T
bobban 329dcab936 Fix agent install/uninstall sudo placement, add dependency install hints
curl ... | API_URL=... bash runs as the invoking user, not root, so
"sudo" has to go right after the pipe (elevating bash) rather than
before curl — the old commands in the app UI, README, and scripts'
own usage comments put it in a spot that still failed the root check.
Also have install.sh/report-tasks.sh suggest the right package-manager
command (apt/dnf/yum/apk/pacman/zypper) when curl or jq is missing
instead of just failing, and bring the README up to date with both.
2026-09-02 23:12:12 +02:00

99 lines
3.1 KiB
Bash

#!/usr/bin/env bash
# Installs the Schedule Task Manager agent as a systemd timer.
#
# Usage (must run as root — if not already root, put sudo right after the
# pipe so it applies to bash, not to curl):
# curl -fsSL https://schedule.example.lan/agent/linux/install.sh | \
# sudo API_URL=https://schedule.example.lan API_TOKEN=stm_xxx bash
#
set -euo pipefail
: "${API_URL:?Set API_URL to your Schedule Task Manager URL, e.g. https://schedule.example.lan}"
: "${API_TOKEN:?Set API_TOKEN to the per-server token generated on the Servers page}"
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc"
SYSTEMD_DIR="/etc/systemd/system"
INTERVAL_MINUTES="${INTERVAL_MINUTES:-15}"
if [[ "$EUID" -ne 0 ]]; then
echo "This installer must be run as root (it installs a systemd timer)." >&2
echo "If you're piping from curl, put sudo right after the pipe so it elevates bash, not curl:" >&2
echo " curl -fsSL ... | sudo API_URL=... API_TOKEN=... bash" >&2
exit 1
fi
suggest_package_install() {
local pkg="$1"
if command -v apt-get >/dev/null 2>&1; then
echo " sudo apt-get update && sudo apt-get install -y $pkg"
elif command -v dnf >/dev/null 2>&1; then
echo " sudo dnf install -y $pkg"
elif command -v yum >/dev/null 2>&1; then
echo " sudo yum install -y $pkg"
elif command -v apk >/dev/null 2>&1; then
echo " sudo apk add $pkg"
elif command -v pacman >/dev/null 2>&1; then
echo " sudo pacman -S $pkg"
elif command -v zypper >/dev/null 2>&1; then
echo " sudo zypper install -y $pkg"
fi
}
for bin in curl jq; do
if ! command -v "$bin" >/dev/null 2>&1; then
echo "Required dependency '$bin' is not installed. Try:" >&2
suggest_package_install "$bin" >&2
exit 1
fi
done
if ! command -v systemctl >/dev/null 2>&1; then
echo "systemctl was not found — this installer requires a systemd-based Linux distribution." >&2
exit 1
fi
echo "Installing Schedule Task Manager agent from $API_URL ..."
curl -fsSL "$API_URL/agent/linux/report-tasks.sh" -o "$INSTALL_DIR/schedule-task-manager-agent.sh"
chmod 755 "$INSTALL_DIR/schedule-task-manager-agent.sh"
umask 077
cat > "$CONFIG_DIR/schedule-task-manager-agent.env" <<EOF
API_URL=$API_URL
API_TOKEN=$API_TOKEN
EOF
chmod 600 "$CONFIG_DIR/schedule-task-manager-agent.env"
cat > "$SYSTEMD_DIR/schedule-task-manager-agent.service" <<EOF
[Unit]
Description=Report scheduled tasks to Schedule Task Manager
[Service]
Type=oneshot
EnvironmentFile=$CONFIG_DIR/schedule-task-manager-agent.env
ExecStart=$INSTALL_DIR/schedule-task-manager-agent.sh
EOF
cat > "$SYSTEMD_DIR/schedule-task-manager-agent.timer" <<EOF
[Unit]
Description=Periodically report scheduled tasks to Schedule Task Manager
[Timer]
OnBootSec=2min
OnUnitActiveSec=${INTERVAL_MINUTES}min
Persistent=true
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now schedule-task-manager-agent.timer
echo "Installed. Running an initial report now..."
"$INSTALL_DIR/schedule-task-manager-agent.sh"
echo "Done. The agent reports every ${INTERVAL_MINUTES} minute(s) via the 'schedule-task-manager-agent.timer' systemd timer."
echo "To remove it later: curl -fsSL $API_URL/agent/linux/uninstall.sh | sudo bash"