script-system-scheduled-restart 1.2 KB

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