Projects: Automatic Keyboard Backlight for Dell XPS in Linux
My Dell XPS 9380 laptop
Last night I finished a fun mini project as part of Unix Tutorials Projects. I have writted a basic enough script that can be added as root cronjob for automatically controlling keyboard backlight on my Dell XPS 9380.
Bash Script for Keyboard Backlight Control
As I’ve written just a couple of days ago, it’s actually quite easy to turn keyboard backlight on or off on a Dell XPS in Linux (and this probably works with other Dell laptops).
Armed with that knowledge, I’ve written the following script:
How My Dell Keyboard Backlight Script Works
This is what my script does when you run it as root (it won’t work if you run as regular user):
it determines the WORKDIR (I defined it as /home/greys/scripts/backlight)
it starts writing log file backlight.log in that $WORKDIR
it checks for lock file backlight.kbd in the same $WORKDIR
it confirms current hour and checks if it’s a rather late hour (when it must be dark). For now I’ve set it between 21 (9pm) and 4 (4am, that is)
if checks current keyboard backlight status ($KDBBACKLIGHT variable)
it compares this status to the desired state (based on which hour that is)
if we need to update keyboard backlight setting, we check for lockfile.
If a recent enough file exists, we skip updates
Otherwise, we set the backlight to new value
all actions are added to the $WORKDIR/backlight.log file
Log file looks like this:
How To Activate Keyboard Backlight cronjob
I have added this script to the root user’s cronjob. In Ubuntu 19.04 running on my XPS laptop, this is how it was done:
I then added the following line:
Depending on where you place similar script, you’ll need to update full path to it from /home/greys/scripts. And then update WORKDIR variable in the script itself.
Keyboard Backlight Unix Tutorial Project Follow Up
Here are just a few things I plan to improve:
see if I can access Ubuntu’s Night Light settings instead of hardcoding hours into the script
fix the timezone – should be IST and not BST for my Dublin, Ireland location
Just for fun, try logging output into one of system journals for journalctl