3

On Windows 10, my laptop's keyboard backlit automatically turned off after a minute or so of inactivity. As soon as any activity occurred, they turned back on.

On ubuntu, if I run the following

echo <0-3> | sudo tee /sys/class/leds/asus::kbd_backlight/brightness 

That works for dimming / increasing the brightness (0 being turned off, 3 being brightest).

Is there a way to have the above automatically get executed whenever the system's been idle for 1 minute, as well as re-run it when it comes back from being idle?

Thanks.

Jacob Vlijm
  • 83,767

2 Answers2

6

What you need to do

  1. Install xprintidle, to keep track of the current idle time (defined by lack of keyboard- and mouse events).
  2. Create a small background script (further below); copy the script into an empty file and save it as dim_keyboard.sh. Make the script executable and copy it to a location where unauthorized users cannot alter the script (e.g. /usr/local/bin).
  3. Add the script to the sudoers file, since you run the command with sudo: Add the line:

    <username> ALL= NOPASSWD: /path/to/your/script
    

    (where <username> is your username) to the file, as explained e.g. here.


N.B. In the sudoers file, for security reasons always use the full path to the script, also if it is in $PATH. The reason is that else it would be easy to place a similarly named script in ~/bin, which could run with sudo privileges, possibly with malicious code.


  1. Test- run the script with the idle time (in seconds) as an argument:

    sudo /path/to/script.sh 60
    

    To dim the backlight after a minute

  2. If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. add the command:

    sudo /path/to/script.sh 60
    

The script:

#!/bin/bash

let "div = 1000"
let "limit = $1"
dimmed=false
cmd=/sys/class/leds/asus::kbd_backlight/brightness

while true
do
  sleep 2
  let "idle = $(xprintidle)"
  if [ $(($idle / $div)) -gt $limit ] && [ $dimmed == false ]; then
    echo 0 | tee $cmd 
    dimmed=true
  elif [ $(($idle / $div)) -le $limit ] && [ $dimmed == true ]; then
    echo 3 | tee $cmd 
    dimmed=false
  fi
done
Jacob Vlijm
  • 83,767
  • /usr/local/bin/dim_keyboard.sh: line 11: let: idle = : syntax error: operand expected (error token is "= ") /usr/local/bin/dim_keyboard.sh: line 12: / 1000: syntax error: operand expected (error token is "/ 1000") – Click Upvote Apr 26 '16 at 11:04
  • @ClickUpvote It works on my system, but the "$(xprintidle)" might be interpreted as a string. could you replace the line by let "idle = $(xprintidle)"? – Jacob Vlijm Apr 26 '16 at 11:12
  • Sorry, I was testing before adding the file to sudoers (even though I was doing sudo dim_keyboard.sh.) After adding to sudoers, it works fantastically :) Thanks! – Click Upvote Apr 26 '16 at 11:34
  • Hmm, on reboot, it asks for password when run, even though I've added username ALL= NOPASSWD: /usr/local/bin/dim_keyboard.sh as the last line of sudoers via sudo visudo. – Click Upvote Apr 26 '16 at 11:46
  • 1
    @ClickUpvote Oh crap! I totally forgot to mention, the script is fine, but the startup command is wrong: should be with sudo. will edit. – Jacob Vlijm Apr 26 '16 at 11:51
  • Also, username ALL should be clickupvote ALL, i.e username should be replaced by the actual username, right? – Click Upvote Apr 26 '16 at 11:55
0

To continue the https://askubuntu.com/a/762994/954857 answer. Do add the following

savedState=$(cat /sys/class/leds/asus::kbd_backlight/brightness)

Use this line to save the older state and reuse so the brightness will always be the same