wake-up-at.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # Function to validate and calculate time until wakeup
  4. calculate_seconds_until_wake() {
  5. local input_time="$1"
  6. wake_time=$(date -d "$input_time" +%s 2>/dev/null)
  7. if [[ $? -ne 0 ]]; then
  8. echo "Invalid time format. Please use formats like 'tomorrow 08:00' or '+10h'."
  9. exit 1
  10. fi
  11. current_time=$(date +%s)
  12. seconds_until_wake=$((wake_time - current_time))
  13. if [[ $seconds_until_wake -le 0 ]]; then
  14. echo "Specified time is in the past. Please provide a future time."
  15. exit 1
  16. fi
  17. }
  18. # Check if rtcwake is available
  19. if ! command -v rtcwake &> /dev/null; then
  20. echo "rtcwake is not installed. Please install it and try again."
  21. exit 1
  22. fi
  23. # Prompt user for wake-up time
  24. WAKEUP_TIME="tomorrow 08:00"
  25. read -erp "Enter the time to wake up: " -i "$WAKEUP_TIME" WAKEUP_TIME
  26. calculate_seconds_until_wake "$WAKEUP_TIME"
  27. # Confirm and execute shutdown
  28. echo "Scheduling shutdown until $WAKEUP_TIME."
  29. echo "The system will power on in $seconds_until_wake seconds."
  30. read -p "Press Enter to proceed or Ctrl+C to cancel..."
  31. # Execute rtcwake command
  32. sudo rtcwake -m off -s "$seconds_until_wake"
  33. # Optional: Print a message after the system shuts down
  34. echo "The system is shutting down and will wake up at $WAKEUP_TIME."