Created
August 9, 2024 13:31
-
-
Save hansyulian/c46539bd014339973a8621cc6e57dd58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
currentState=0 | |
state1StartHour=4 | |
state2StartHour=22 | |
function set_sleep(){ | |
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/blank-on-ac -s $1 | |
now=$(date +"%D %T") | |
echo "set sleep $1 $now" >> ~/cron.log | |
} | |
function update_state(){ | |
currentHour=$(date +%-H) | |
if [ $currentHour -ge $state1StartHour ] && [ $currentHour -lt $state2StartHour ]; then | |
currentState=1 | |
else | |
currentState=2 | |
fi | |
} | |
while true; do | |
currentHour=$(date +%-H) | |
currentMinute=$(date +%-M) | |
waitHour=0 | |
waitTime=0 | |
update_state | |
echo "Current state: $currentState" | |
if [[ "$currentState" == "1" ]]; then | |
echo "Executing disable sleep" | |
set_sleep 0 | |
waitHour=$((state2StartHour - currentHour)) | |
else | |
echo "Executing enable sleep" | |
set_sleep 1 | |
waitHour=$((state1StartHour - currentHour)) | |
fi | |
if [[ $waitHour -lt 0 ]]; then | |
waitHour=$((waitHour + 24)) | |
fi | |
waitTime=$((waitHour*3600 - currentMinute*60)) | |
echo "Waiting for $waitTime seconds until the next script execution." | |
sleep $waitTime | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment