You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 -e and add this line (replace the path with your script's actual location):
    * * * * * /home/your-user/scripts/terrarium-light.sh >> /var/log/terrarium-light.log 2>&1
    
    This executes the script every minute and logs output for debugging.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:32:41