script-system-scheduled-restart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bash
  2. # Update and restart the system
  3. # Copyright 2021 Bryan C. Roessler
  4. parent="${BASH_SOURCE[0]}"
  5. parent=${parent%/*}
  6. [[ -f $parent/script-functions ]] && . "$parent"/script-functions || exit 1
  7. is_root
  8. [[ $# -eq 0 ]] && time='*-*-* 01:30:00' # 1:30AM
  9. [[ $# -gt 1 ]] && time="$*"
  10. script-system-update
  11. ask_ok "Set a scheduled reboot for $time?" || exit 1
  12. cat <<- EOF > "/usr/lib/systemd/system/scheduled-reboot.timer"
  13. [Unit]
  14. Description=Scheduled reboot
  15. [Timer]
  16. OnCalendar=$time
  17. Unit=reboot.target
  18. [Install]
  19. WantedBy=timers.target
  20. EOF
  21. systemctl daemon-reload
  22. systemctl start scheduled-reboot.timer
  23. # Current date
  24. dt=$(date '+%d/%m/%Y %H:%M:%S');
  25. message="System restart scheduled for $time. The current time is $dt. Make sure all changes are saved."
  26. # Graphical notification
  27. IFS=$'\n'
  28. for LINE in $(w -hs); do
  29. USER=$(echo "$LINE" | awk '{print $1}')
  30. USER_ID=$(id -u "$USER")
  31. DISP_ID=$(echo "$LINE" | awk '{print $8}')
  32. sudo -u "$USER" DISPLAY="$DISP_ID" DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$USER_ID"/bus notify-send "$message" --icon=dialog-warning
  33. done
  34. # Wall notification
  35. wall -n "$message"
  36. exit $?