#!/usr/bin/env bash set -euo pipefail SERVICE_NAME="daily-reboot.service" TIMER_NAME="daily-reboot.timer" SERVICE_PATH="/etc/systemd/system/${SERVICE_NAME}" TIMER_PATH="/etc/systemd/system/${TIMER_NAME}" ON_CALENDAR="*-*-* 02:30:00 Asia/Shanghai" require_root() { if [[ ${EUID:-$(id -u)} -ne 0 ]]; then echo "请使用 root 或 sudo 运行此脚本。" >&2 exit 1 fi } require_systemd() { if ! command -v systemctl >/dev/null 2>&1; then echo "未找到 systemctl,当前系统似乎不是 systemd 环境。" >&2 exit 1 fi } write_unit_files() { cat > "$SERVICE_PATH" < "$TIMER_PATH" </dev/null systemctl restart "$TIMER_NAME" echo "已安装/更新:${TIMER_NAME}" echo "执行时间:北京时间每天 02:30(Asia/Shanghai)" echo systemctl list-timers "$TIMER_NAME" --all --no-pager || true } uninstall_units() { require_root require_systemd systemctl disable --now "$TIMER_NAME" >/dev/null 2>&1 || true systemctl stop "$SERVICE_NAME" >/dev/null 2>&1 || true rm -f "$SERVICE_PATH" "$TIMER_PATH" systemctl daemon-reload systemctl reset-failed "$SERVICE_NAME" "$TIMER_NAME" >/dev/null 2>&1 || true echo "已卸载:${SERVICE_NAME} / ${TIMER_NAME}" } usage() { cat <&2 exit 1 ;; esac } main() { case "${1:-}" in install|update|-i|--install|--update) install_or_update ;; uninstall|-u|--uninstall) uninstall_units ;; "") interactive_menu ;; -h|--help|help) usage ;; *) echo "未知参数:$1" >&2 usage exit 1 ;; esac } main "$@"