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?



2 Answers 2


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:


let "div = 1000"
let "limit = $1"

while true
  sleep 2
  let "idle = $(xprintidle)"
  if [ $(($idle / $div)) -gt $limit ] && [ $dimmed == false ]; then
    echo 0 | tee $cmd 
  elif [ $(($idle / $div)) -le $limit ] && [ $dimmed == true ]; then
    echo 3 | tee $cmd 
  • /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") Apr 26, 2016 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)"? Apr 26, 2016 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! Apr 26, 2016 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. Apr 26, 2016 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. Apr 26, 2016 at 11:51

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

You must log in to answer this question.

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