Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active October 7, 2018 07:13
Show Gist options
  • Save ThinGuy/197d3633a9ce3503eb7095e85c9b9d9a to your computer and use it in GitHub Desktop.
Save ThinGuy/197d3633a9ce3503eb7095e85c9b9d9a to your computer and use it in GitHub Desktop.
Set keyboard backlight brightness at startup via a systemd service
#!/bin/bash
# This script creates a systemd service to turn up
# the keyboard backlight at startup as it's not
# respecting the last setting
# In order to do this, we have to change the perms,
# then echo a value into the brightness file.
# The latter has to be done via a script as just
# running the echo from systemd does not work.
SNAME=keyboard-backlight.service
# Try to find the keyboard backlight brightness file
KBL=$(find 2>/dev/null $(find 2>/dev/null /sys/class/leds/ -type l -iname "*k*backlight")/ -type f -iname "bright*")
# Exit if we can't find the file
[[ -f ${KBL} ]] || { printf "\e[2GCould not find the keyboard backlight brightness file\n";exit 1; }
# Remove old files - This is cleanup from testing different options. Decided to leave it in script
if [[ -n $(systemctl list-unit-files --type=service ${SNAME} --no-legend) ]];then
sudo systemctl 2>/dev/null daemon-reload
sudo systemctl 2>/dev/null stop ${SNAME}
sudo systemctl 2>/dev/null disable ${SNAME}
fi
[[ -f /usr/local/bin/${SNAME//service/sh} ]] && sudo rm 2>/dev/null -rf /usr/local/bin/${SNAME//service/sh}
# Create new script to call from the systemd service
printf -- '#!/bin/bash\n/bin/echo 100 > '"${KBL}"'\n'|sudo tee /usr/local/bin/${SNAME//service/sh}
sudo chmod 2>/dev/null +x /usr/local/bin/${SNAME//service/sh}
sleep 2
# Create a oneshot systemd service
cat <<EOF|sudo tee /etc/systemd/system/${SNAME}
[Unit]
Description=Turns Keyboard Backlight On at Startup
After=dbus.service
ConditionPathExists=${KBL}
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/bin/chmod 666 ${KBL}
ExecStart=/usr/local/bin/${SNAME//service/sh}
ExecStartPost=/bin/chmod 444 ${KBL}
[Install]
WantedBy=multi-user.target
EOF
# Start and enable systemd service
sudo systemctl start ${SNAME}
sudo systemctl enable ${SNAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment