Forcing shutdowns
2024-08-20 19:49
My post yesterday on smol.pub:
My Health Improvement Practitioner (HIP) recommended that I shut down my computer by 8pm each night to help me get better rest.
I then proceeded to work on a bash script that shuts down at an input time, with a notification and dimming of the screen.
I was working on it till 9.15pm.
Anyway, I finished it tonight. First, I had to install brightnessctl and at:
$ sudo apt install brightnessctl
$ sudo apt install at
brightnessctl controls the brightness level. The below command dims the brightness to 40%:
$ sudo brightnessctl -d "intel_backlight" set 40%
at enables me to schedule the command.
The script uses shutdown to shutdown and notify-send to create a popup notification 10 minutes prior. Unfortunately I couldn't get the brightness to work from inside the script. I tried all combos of /", single or double apostrophes, dropping sudo, calling directly from the path, and some other bits and bobs, including xgamma but no luck.
I copied the script to /bin:
$ sudo cp set_shutdown.sh /usr/local/bin/
And added a goodnight alias to ~/.bashrc:
alias goodnight='set_shutdown.sh'
Now when I type in goodnight it will prompt me for the time I want to shutdown:
$ goodnight
What time? (HH:MM): 20:00
Shutdown scheduled for Tue 2024-08-20 20:00:00 NZST, use 'shutdown -c' to cancel.
warning: commands will be executed using /bin/sh
job 27 at Tue Aug 20 19:50:00 2024
warning: commands will be executed using /bin/sh
job 28 at Tue Aug 20 19:50:00 2024
Shutdown scheduled for 20:00. Brightness reduction and notification scheduled for 19:50.
The script (I admit I used ChatGPT to help calculate time-10m):
#!/bin/bash
# Prompt to enter the shutdown time in HH:MM format
read -p "What time? (HH:MM): " sd
# Schedule the shutdown
sudo shutdown "$sd"
# Convert HH:MM to minutes
sd_hour=$(echo "$sd" | cut -d: -f1)
sd_minute=$(echo "$sd" | cut -d: -f2)
sd_mins=$((10#$sd_hour * 60 + 10#$sd_minute))
# Subtract 10 minutes for warning time
wt_minutes=$((sd_mins - 10))
# Convert back to HH:MM
wt_hour=$(printf "%02d" $((wt_minutes / 60)))
wt_minute=$(printf "%02d" $((wt_minutes % 60)))
wt="${wt_hour}:${wt_minute}"
# Schedule the things
echo "notify-send -u critical 'Oh hey' 'Time to wrap up. Shutting down in 10 minutes...'" | at "$wt"
echo "sudo brightnessctl -d \"intel_backlight\" set 40%" | at "$wt"
echo "Shutdown scheduled for $sd. Brightness reduction and notification scheduled for $wt."
(issues with the second-last line)
The notification, which blurts out a cute little Linux "ping":
Hopefully I won't use the shutdown -c command too often! Obvious floor in my system - human intervention. Anyway I learnt a bunch of stuff - at, shutdown, notify-send, brightnessctl, more familiarisation with bin scripts and .bashrc.
Would you do this differently? Do you have something similar? Do you know how I could get the dimming to work? Please get in touch.
$ goodnight