Terrarium定时USB插座控制脚本求助:定时逻辑无法正常运行
Fixing Your Terrarium Light Control Script
Hey there! Let's break down why your current logic isn't working, and how to get your terrarium light schedule running as intended.
The Core Issue
Your current condition [ $hour -ge $light_on ] && [ $hour -lt $light_off ] tries to check if the hour falls between 22 and 13—but since this is a cross-midnight time range, this condition will never be true (22 is always larger than 13). Instead, you need to split this into two valid, non-overlapping time windows that cover your desired schedule:
- From 22:00 to 23:59 (same day)
- From 00:00 to 12:59 (next day)
Corrected Script Logic
Here's the fixed code with proper logic for your 22-on / 13-off schedule:
# Define your schedule (24-hour format) light_on=22 light_off=13 # Get current hour (formatted as 00-23) hour=$(date +%H) # Check if we're in the "light on" window (22:00 to next day 13:00) if [ $hour -ge $light_on ] || [ $hour -lt $light_off ]; then # Command to turn the light ON echo "Turning terrarium light ON" # Replace with your actual USB socket control command, e.g.: # ./usb-socket-controller --activate else # Command to turn the light OFF echo "Turning terrarium light OFF" # Replace with your actual USB socket control command, e.g.: # ./usb-socket-controller --deactivate fi
Extra Tips to Ensure Reliability
- Automate with Cron: To make this script run automatically, add it to your system's crontab so it checks the time regularly. Run
crontab -eand add this line (replace the path with your script's actual location):
This executes the script every minute and logs output for debugging.* * * * * /home/your-user/scripts/terrarium-light.sh >> /var/log/terrarium-light.log 2>&1 - Verify Timezone: Double-check your system's timezone with
date—if the system time doesn't match your local time, the schedule will be off. - Test the Commands: Make sure your USB socket control commands work on their own before adding them to the script.
内容的提问来源于stack exchange,提问作者Amar
相关产品推荐
相关产品推荐

