3

Question

How to ensure keyboard backlight is on (at max) at boot time on a Dell laptop?

Rationale

I sometimes hit the combination of keys which turns off the keyboard backlight at day time not noticing it, and then when I wake up, in the middle of the night usually, I don't want to wake up my wife by turning the lights on. Hence, I seek a solution, which would read the maximum possible value of the backlight and setting it no matter what when I boot my computer up. I always turn it off at night so the solution does not have to account for the sleep or hibernate modes.

Research

The maximum value of keyboard backlight is stored in:

/sys/class/leds/dell\:\:kbd_backlight/max_brightness

And the actual value of currently set value is stored in:

/sys/class/leds/dell\:\:kbd_backlight/brightness

2 Answers 2

2

No matter that the maximum value should most probably be constant, I don't know why or how but at previous boot I had the maximum value of 3. Now I have 2. I'm confused and baffled at the same time.

I don't want to search for a reason, such as BIOS setting, let's just read the maximum value at boot time and set it. No matter if during the day I accidentally turned the backlight possibly off.

I came up with a direct approach using:

sudo crontab -e

And reading and setting the maximum value in one command:

@reboot /bin/cat /sys/class/leds/dell\:\:kbd_backlight/max_brightness > /sys/class/leds/dell\:\:kbd_backlight/brightness
1

I did something a bit more malleable (i.e. configurable): I created a init script that is automatically started at boot time except when runlevel is either 0 (the system is entering halt/poweroff mode) or 6 (the system is entering reboot/restart mode).

When it's started, this script checks if local time is something between 17:00:00 and 07:59:59: if it is, it turns the keyboard backlight on; if it's not, it turns such backlight off.

What follows are the required steps to create such script.


STEP 1 - CREATE THE INIT SCRIPT

Open a shell terminal emulation window (e.g. press CtrlAltT) and then use a graphical text editor such as Gedit or Mousepad to create the init script. I'm using Gedit, so the command that I executed at the terminal was this:

sudo gedit /etc/init.d/keyboard-backlight-brightness

Copy the source-code below and paste it into the text editor's window:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          keyboard-backlight-brightness
# Required-Start:
# Required-Stop:
# Default-Start:     1 2 3 4 5
# Default-Stop:      0 6
# Short-Description: Sets keyboard backlight brightness at boot time
# Description:       Script that sets the keyboard's LED (backlight) brightness at boot time
### END INIT INFO

case "$1" in
    start)
       # If the current runlevel is one of those in 'Default-Start', then
       # sets keyboard backlight to 24 (from a set of 0, 12, 24, 36,..., 255) if local time
       # is between 17:00:00 and 07:59:59, otherwise set it to 0 (e.g. 'off').
       if [[ `date +%-H` > 16 || `date +%-H` < 8 ]]
       then
            echo -n "Setting keyboard backlight brightness to 24..."
            echo 24 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
       else
            echo -n "Setting keyboard backlight brightness to 0..."
            echo 0 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
       fi
    ;;

    stop)
       # If the current runlevel is one of those in 'Default-Stop', then
       # sets keyboard backlight to 0 (i.e. 'off').
       echo -n "Setting keyboard backlight brightness to 0..."
       echo 0 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
    ;;

    reload|restart)
       $0 stop
       $0 start
    ;;

    *)
       echo "Usage: keyboard-backlight-brightness start|stop|restart|reload"
       exit 1
esac

exit 0

In the case of my computer (a 13" mid-2009 Apple Macbook Pro), its keyboard's brightness file is located at /sys/class/leds/smc\:\:kbd_backlight/brightness, but I changed the codes in this answer so they all show your /sys/class/leds/dell\:\:kbd_backlight/brightness file instead.

You may change that 24 (brightness intensity level) to whatever you want between 1 and 255, and you may also set a different local time range. E.g. in order to turn the backlight on between 18:00:00 and 05:59:59, use this code:

if [[ `date +%-H` > 17 || `date +%-H` < 6 ]]

In case you definitely want the keyboard backlight to remain on and at full power, despite of the time of the day (or night), you may use this code, instead:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          keyboard-backlight-brightness
# Required-Start:
# Required-Stop:
# Default-Start:     1 2 3 4 5
# Default-Stop:      0 6
# Short-Description: Sets keyboard backlight brightness at boot time
# Description:       Script that sets the keyboard's LED (backlight) brightness at boot time
### END INIT INFO

case "$1" in
    start)
       # If the current runlevel is one of those in 'Default-Start', then
       # sets keyboard backlight to 255 (from a set of 0, 12, 24, 36,..., 255).
       echo -n "Setting keyboard backlight brightness to 255..."
       echo 255 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
    ;;

    stop)
       # If the current runlevel is one of those in 'Default-Stop', then
       # sets keyboard backlight to 0 (i.e. 'off').
       echo -n "Setting keyboard backlight brightness to 0..."
       echo 0 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
    ;;

    reload|restart)
       $0 stop
       $0 start
    ;;

    *)
       echo "Usage: keyboard-backlight-brightness start|stop|restart|reload"
       exit 1
esac

exit 0

After making the desired adjustments in the code, save the file and exit the text editor.

STEP 2 - CONFIGURE THE INIT SCRIPT'S PERMISSIONS

You just have to execute:

sudo chmod 755 /etc/init.d/keyboard-backlight-brightness

...and the init script will allow rwx (read, write and execute) access for the root user and rx (read and execute) access for everybody else.

STEP 3 - CREATE THE INIT SCRIPT'S LINKS TO THE CORRESPONDING SYSTEM V RUNLEVELS

You just have to execute:

sudo update-rc.d keyboard-backlight-brightness defaults

...and the init script will be linked to the corresponding runlevels where it must be executed during boot time.

STEP 4 - START THE INIT SCRIPT

Just run:

sudo service keyboard-backlight-brightness start

...and the service will be started.


If you decided to keep the local time range option in the code, then if by the time that you start the script your local time happens to be in the range specified in the script, the keyboard backlight will light up, otherwise such backlight will be turned off.

You may want to restart your system, just to make sure that the script is indeed being started (and working) at boot time:

sudo telinit 6

or

sudo shutdown -r now

or

sudo reboot

or

sudo init 6

IF THE INIT SCRIPT DOESN'T WORK PROPERLY OR JUST DOESN'T WORK

This happened to me after I upgraded my Linux distribution: it replaced init with systemd. In such case, you need to:

  1. Stop the init script:

    sudo service keyboard-backlight-brightness stop
    
  2. Then remove the script from the init system:

    sudo update-rc.d -f keyboard-backlight-brightness remove
    
  3. Then delete the init script file:

    sudo rm -f /etc/init.d/keyboard-backlight-brightness
    

Once the 3 steps above are done, let's create a systemd script and service.


STEP 1 - CREATE A CUSTOM NESTED SH SCRIPT

At the terminal, run this:

sudo gedit /usr/bin/keyboard-backlight-brightness.sh

...and then paste the code below inside of the new file (notice that this is not a bash script but, instead, it has to be an sh script):

#!/bin/sh
# Sets keyboard backlight brightness to 24 (from a set of
# 0, 12, 24, 36,..., 255) if local time is between
# 17:00:00 and 07:59:59, otherwise sets it to 0 (e.g. 'off').

if [ $(date +%-H) -gt 16 -o $(date +%-H) -lt 8 ]
then
    echo -n "Setting keyboard backlight brightness to 24..."
    echo 24 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
else
    echo -n "Setting keyboard backlight brightness to 0..."
    echo 0 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness
fi

exit 0

Alternatively, if you really want continuous full-power keyboard backlight, then use this code instead:

#!/bin/sh
# Sets keyboard backlight brightness to 255 (i.e. 100%).

echo -n "Setting keyboard backlight brightness to 255..."
echo 255 | tee /sys/class/leds/dell\:\:kbd_backlight/brightness

exit 0

Once you're done pasting the code, save the file and exit the text editor, then change such file's permissions:

sudo chmod 755 /usr/bin/keyboard-backlight-brightness.sh

Now that you created an sh script keyboard-backlight-brightness.sh inside of /usr/bin, it's time to nest it inside of a systemd service script that must however be created first.

STEP 2 - CREATE THE SYSTEMD SCRIPT AND THEN NEST THE SH SCRIPT INSIDE OF IT

At the terminal, run this:

sudo gedit /etc/systemd/system/keyboard-backlight-brightness.service

...and then paste this code inside of the new file:

[Unit]
Description=Sets keyboard backlight brightness at boot time

[Service]
ExecStart=/usr/bin/keyboard-backlight-brightness.sh

[Install]
WantedBy=multi-user.target graphical.target rescue.target

(notice that the keyboard-backlight-brightness.sh script is nested through the ExecStart parameter. Also, the options at WantedBy cause the systemd script to always be executed - except when the system is either rebooting or turning the computer off)

Once you're done pasting the code, save the file and exit the text editor, then change such file's permissions:

sudo chmod 755 /etc/systemd/system/keyboard-backlight-brightness.service

STEP 3 - RELOAD THE SYSTEMD CONFIGURATION AND THEN ENABLE THE NEW SYSTEMD SERVICE SCRIPT

At the terminal, run the command below in order to incorporate the new systemd service script into the systemd environment:

sudo systemctl daemon-reload

...and then run the command below so such new service keyboard-backlight-brightness.service loads at boot time:

sudo systemctl enable keyboard-backlight-brightness.service

STEP 4 - START THE SYSTEMD SERVICE SCRIPT

Just run:

sudo systemctl start keyboard-backlight-brightness.service

...and the service will be started.


If you decided to keep the local time range option in the code, then if by the time that you start the systemd service script your local time happens to be in the range specified in the script, the keyboard backlight will light up, otherwise such backlight will be turned off.

You may want to restart your system, just to make sure that the script is indeed being started (and working) at boot time:

sudo telinit 6

or

sudo shutdown -r now

or

sudo reboot

or

sudo init 6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .