I wrote a bash script to run at start-up and set an alarm for Monday through Friday. It's not needed on Saturday or Sunday, so I modified the script to save some energy on those days. The script works, kinda. But, it always goes with the first 'if' statement. The computer wakes every day at the specified time, and fails to execute the '== Friday' and '== Saturday' option at start-up only. If I run the script manually the '== Friday' and '== Saturday' options are properly screened.
Any ideas as to why this is happening. I am concerned about the reliability of the script.
#!/bin/bash
# see if waiting for the system to start helps get the correct time
sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sleep 10
nvidia-settings -a [gpu:0]/GPUFanControlState=1
nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=15
day=$(date +%A)
#printf "%s\n" "$day"
#DT=$(date --date='06:19 tomorrow')
if [[ "$day" == "Sunday" || "$day" == "Monday" || "$day" == "Tuesday" || "$day" == "Wednesday" || "$day" == "Thursday" ]];
then
DTs=$(date --date='06:19:00 tomorrow +22 seconds' +%s)
fi
if [[ "$day" == "Friday" || "$day" == "Saturday" ]];
then
DTs=$(date --date='06:19:00 next Monday' +%s)
fi
# The RTCWake needs to be set to 0 so that an IO error is not created
sudo sh -c "echo $DTs > /sys/class/rtc/rtc0/wakealarm"
WAKERTC=$(cat /sys/class/rtc/rtc0/wakealarm)
WAKERTC=$(date -d @$WAKERTC)
notify-send "will wake $WAKERTC"
notify-send "NVIDIA Fan set to 15"
FYI, the additional 22 seconds allows me to debug the script. Also, the 'echo 0' statement was moved early in the script as I though that some delay in the boot might help the clock (it didn't).
EDIT ADDED --- Thanks for the suggestions regarding the best practices for the if-then code. They were good catches that I should have seen myself. Though, the script still misbehaves. Either the if statement is not executed at all, producing a notify statement the reads "will wake ", meaning no alarm was set, or Friday/Saturday query is not recognized and it still wakes the next day. IMPORTANT NOTES: You might notice that the script is interlaced with an Nvidia GPU fan controller. This executes everytime. Also the script is perfect if run from terminal, but only works about 80% of the time if run from the start-up applications.
Try this:
if [[ "$day" == "Sunday" || "$day" == "Monday" || "$day" == "Tuesday" || "$day" == "Wednesday" || "$day" == "Thursday" ]]; then
DTs=$(date --date='06:19:00 tomorrow +22 seconds' +%s)
else
DTs=$(date --date='06:19:00 next Monday' +%s)
fi
The second if
seemed redundant when you can use else