feat: change timing of shutdowns

This commit is contained in:
Krzysztof kuhy Rudnicki 2025-07-27 20:05:05 +02:00
parent 59b1c21b46
commit 9961dd8673

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Script to set up automatic PC shutdown with day-specific time windows # Script to set up automatic PC shutdown with day-specific time windows
# Saturday-Thursday: Shutdown between 21:30-05:00 # Monday-Wednesday: Shutdown between 21:00-05:00
# Friday/Sunday: Shutdown between 00:00-05:00 # Thursday-Sunday: Shutdown between 22:00-05:00
# Handles sudo privileges automatically # Handles sudo privileges automatically
set -e # Exit on any error set -e # Exit on any error
@ -18,8 +18,8 @@ show_usage() {
echo " status - Show current status" echo " status - Show current status"
echo "" echo ""
echo "Shutdown Schedule:" echo "Shutdown Schedule:"
echo " Saturday-Thursday: 21:30-05:00" echo " Monday-Wednesday: 21:00-05:00"
echo " Friday/Sunday: 00:00-05:00" echo " Thursday-Sunday: 22:00-05:00"
echo "" echo ""
} }
@ -178,8 +178,8 @@ show_current_status() {
echo "" echo ""
echo "Shutdown Schedule:" echo "Shutdown Schedule:"
echo " Saturday-Thursday: 21:30-05:00" echo " Monday-Wednesday: 21:00-05:00"
echo " Friday/Sunday: 00:00-05:00" echo " Thursday-Sunday: 22:00-05:00"
echo "" echo ""
} }
@ -222,6 +222,7 @@ Description=Timer for automatic PC shutdown with day-specific windows
Requires=day-specific-shutdown.service Requires=day-specific-shutdown.service
[Timer] [Timer]
OnCalendar=*-*-* 21:00:00
OnCalendar=*-*-* 21:30:00 OnCalendar=*-*-* 21:30:00
OnCalendar=*-*-* 22:00:00 OnCalendar=*-*-* 22:00:00
OnCalendar=*-*-* 22:30:00 OnCalendar=*-*-* 22:30:00
@ -283,8 +284,8 @@ show_status() {
echo "" echo ""
echo "Shutdown Schedule:" echo "Shutdown Schedule:"
echo " Saturday-Thursday: 21:30-05:00" echo " Monday-Wednesday: 21:00-05:00"
echo " Friday/Sunday: 00:00-05:00" echo " Thursday-Sunday: 22:00-05:00"
echo "" echo ""
echo "Next scheduled checks:" echo "Next scheduled checks:"
@ -313,8 +314,8 @@ case "$1" in
echo " logs - Show recent shutdown logs" echo " logs - Show recent shutdown logs"
echo "" echo ""
echo "Shutdown Schedule:" echo "Shutdown Schedule:"
echo " Saturday-Thursday: 21:30-05:00" echo " Monday-Wednesday: 21:00-05:00"
echo " Friday/Sunday: 00:00-05:00" echo " Thursday-Sunday: 22:00-05:00"
echo "" echo ""
show_status show_status
;; ;;
@ -337,8 +338,8 @@ create_shutdown_check_script() {
#!/bin/bash #!/bin/bash
# Smart day-specific shutdown check script # Smart day-specific shutdown check script
# Different shutdown windows based on day of week: # Different shutdown windows based on day of week:
# Saturday-Thursday: 21:30-05:00 # Monday-Wednesday: 21:00-05:00
# Friday/Sunday: 00:00-05:00 # Thursday-Sunday: 22:00-05:00
# Get current time and day # Get current time and day
current_hour=$(date +%H) current_hour=$(date +%H)
@ -348,7 +349,7 @@ day_of_week=$(date +%u) # 1=Monday, 7=Sunday
day_name=$(date +%A) day_name=$(date +%A)
# Convert time to minutes for easier comparison # Convert time to minutes for easier comparison
# 21:30 = 1290 minutes, 05:00 = 300 minutes # 21:00 = 1260 minutes, 22:00 = 1320 minutes, 05:00 = 300 minutes
# 00:00 = 0 minutes, 05:00 = 300 minutes # 00:00 = 0 minutes, 05:00 = 300 minutes
logger -t day-specific-shutdown "Checking shutdown conditions at $(date) - Day: $day_name ($day_of_week), Time: $current_hour:$current_minute" logger -t day-specific-shutdown "Checking shutdown conditions at $(date) - Day: $day_name ($day_of_week), Time: $current_hour:$current_minute"
@ -356,31 +357,37 @@ logger -t day-specific-shutdown "Checking shutdown conditions at $(date) - Day:
# Determine if we should shutdown based on day and time # Determine if we should shutdown based on day and time
should_shutdown=false should_shutdown=false
if [[ $day_of_week -eq 5 ]] || [[ $day_of_week -eq 7 ]]; then if [[ $day_of_week -ge 1 ]] && [[ $day_of_week -le 3 ]]; then
# Friday (5) or Sunday (7): shutdown window 00:00-05:00 # Monday (1), Tuesday (2), Wednesday (3): shutdown window 21:00-05:00
logger -t day-specific-shutdown "Today is $day_name - checking 00:00-05:00 window" logger -t day-specific-shutdown "Today is $day_name - checking 21:00-05:00 window"
if [[ $current_time_minutes -ge 0 ]] && [[ $current_time_minutes -le 300 ]]; then # Check if time is between 21:00 (1260 minutes) and 23:59 (1439 minutes)
should_shutdown=true
logger -t day-specific-shutdown "Time $current_hour:$current_minute is within Friday/Sunday shutdown window (00:00-05:00)"
else
logger -t day-specific-shutdown "Time $current_hour:$current_minute is outside Friday/Sunday shutdown window (00:00-05:00)"
fi
else
# Monday(1), Tuesday(2), Wednesday(3), Thursday(4), Saturday(6): shutdown window 21:30-05:00
logger -t day-specific-shutdown "Today is $day_name - checking 21:30-05:00 window"
# Check if time is between 21:30 (1290 minutes) and 23:59 (1439 minutes)
# OR between 00:00 (0 minutes) and 05:00 (300 minutes) # OR between 00:00 (0 minutes) and 05:00 (300 minutes)
if [[ $current_time_minutes -ge 1290 ]] || [[ $current_time_minutes -le 300 ]]; then if [[ $current_time_minutes -ge 1260 ]] || [[ $current_time_minutes -le 300 ]]; then
should_shutdown=true should_shutdown=true
if [[ $current_time_minutes -ge 1290 ]]; then if [[ $current_time_minutes -ge 1260 ]]; then
logger -t day-specific-shutdown "Time $current_hour:$current_minute is within evening shutdown window (21:30-23:59)" logger -t day-specific-shutdown "Time $current_hour:$current_minute is within evening shutdown window (21:00-23:59)"
else else
logger -t day-specific-shutdown "Time $current_hour:$current_minute is within morning shutdown window (00:00-05:00)" logger -t day-specific-shutdown "Time $current_hour:$current_minute is within morning shutdown window (00:00-05:00)"
fi fi
else else
logger -t day-specific-shutdown "Time $current_hour:$current_minute is outside shutdown window (21:30-05:00)" logger -t day-specific-shutdown "Time $current_hour:$current_minute is outside shutdown window (21:00-05:00)"
fi
else
# Thursday (4), Friday (5), Saturday (6), Sunday (7): shutdown window 22:00-05:00
logger -t day-specific-shutdown "Today is $day_name - checking 22:00-05:00 window"
# Check if time is between 22:00 (1320 minutes) and 23:59 (1439 minutes)
# OR between 00:00 (0 minutes) and 05:00 (300 minutes)
if [[ $current_time_minutes -ge 1320 ]] || [[ $current_time_minutes -le 300 ]]; then
should_shutdown=true
if [[ $current_time_minutes -ge 1320 ]]; then
logger -t day-specific-shutdown "Time $current_hour:$current_minute is within evening shutdown window (22:00-23:59)"
else
logger -t day-specific-shutdown "Time $current_hour:$current_minute is within morning shutdown window (00:00-05:00)"
fi
else
logger -t day-specific-shutdown "Time $current_hour:$current_minute is outside shutdown window (22:00-05:00)"
fi fi
fi fi
@ -469,8 +476,8 @@ show_instructions() {
echo "✓ Timer enabled and started" echo "✓ Timer enabled and started"
echo "" echo ""
echo "Shutdown Schedule:" echo "Shutdown Schedule:"
echo " Saturday-Thursday: 21:30-05:00 (evening to early morning)" echo " Monday-Wednesday: 21:00-05:00 (9:00 PM to 5:00 AM)"
echo " Friday/Sunday: 00:00-05:00 (midnight to early morning)" echo " Thursday-Sunday: 22:00-05:00 (10:00 PM to 5:00 AM)"
echo "" echo ""
echo "Management commands:" echo "Management commands:"
echo " sudo day-specific-shutdown-manager.sh status - Check status" echo " sudo day-specific-shutdown-manager.sh status - Check status"
@ -494,8 +501,8 @@ confirm_setup() {
echo "This will set up your PC to automatically shutdown during specific time windows." echo "This will set up your PC to automatically shutdown during specific time windows."
echo "" echo ""
echo "Shutdown Schedule:" echo "Shutdown Schedule:"
echo " Saturday-Thursday: 21:30-05:00 (9:30 PM to 5:00 AM)" echo " Monday-Wednesday: 21:00-05:00 (9:00 PM to 5:00 AM)"
echo " Friday/Sunday: 00:00-05:00 (Midnight to 5:00 AM)" echo " Thursday-Sunday: 22:00-05:00 (10:00 PM to 5:00 AM)"
echo "" echo ""
echo "Important considerations:" echo "Important considerations:"
echo "- Any unsaved work will be lost during shutdown windows" echo "- Any unsaved work will be lost during shutdown windows"