Make Ubuntu remember screen brightness level without xbacklight

7,638

Solution 1

Actually, I would like to make my Ubuntu remember the level of brightness that was used last. How exactly can I do that? Here's some information:

Introduction

The script below addresses OP's need to store and restore screen brightness that was used last. It works in conjunction with the lightdm greeter and is activated by lightdm. There's no requirement to use lightdm, so if you prefer to use a cron job, you can do so.

Basic idea:

  1. Have the script stored somewhere (either by getting it here or through github)
  2. Create /etc/lightdm/lightdm.conf using the sudo privilege.
  3. Make sure that file has 3 lines: [SeatDefaults] , display-setup-script, and display-stopped script. Look below for details. Script header also gives overview.

Script Source

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# Date: March 7th, 2016
# Purpose: Script that will remember screen brightness
#          Must be used in conjunction with lightdm
#          Place the following 5 lines into /etc/lightdm/lightdm.conf
#
#           [SeatDefaults]
#           #display-setup-script = Script to run when starting a greeter session (runs as root)
#           display-setup-script = /home/USER/bin/sergrep/brightness.sh restore
#           #display-stopped-script = Script to run after stopping the display server (runs as root)
#           display-stopped-script = /home/USER/bin/sergrep/brightness.sh store
#
#           Basic idea is that you must give full path and either store or restore as an option 
# Written for: http://askubuntu.com/q/739654/295286
# Tested on:  Ubuntu 14.04 LTS
# Version: 1.2 , added brightness limit, file creation
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.
ARGV0=$0
ARGC=$#



store()
{
   cat "$SYSDIR"/*/actual_brightness > "$1"
}
#----------

# This function restores brightness. We avoid 
# setting brightness to complete 0, hence
# minimum is 10% that can be restored.

restore()
{
  MAX=$(cat "$SYSDIR"/*/max_brightness  )
  LIMIT=$((MAX/10)) # get approx 10 percent value
  VAL=$(cat "$1" )
  if [ "$VAL" -lt "$LIMIT"  ] ;
  then
       # avoid going bellow 10% of brightness
       # we don't want user's screen to be completely dark
       echo "$LIMIT" > "$SYSDIR"/*/brightness
  else
       echo "$VAL" > "$SYSDIR"/*/brightness
  fi
}
#------------

# This function works for initial run of the script; the script cannot set
# brightness unless datafile exists first, so here we create the file
# Initial value there will be whatever the current brightness on first
# reboot was

create_datafile()
{
  cat "$SYSDIR"/*/actual_brightness > "$1" 
}

puke(){
    printf "%s\n" "$@" > /dev/stderr
    exit 1
}

main()
{
  local DATAFILE="/opt/.last_brightness"
  local SYSDIR="/sys/class/backlight" # sysfs location of all the data

  # Check pre-conditions for running the script
  if [ "$ARGC" -ne 1  ];then
     puke "Script requires 1 argument"
  fi

  if [ $(id -u) -ne 0 ]   ; then
     puke "Script has to run as root"
  fi

  # ensure datafile exists
  [ -f "$DATAFILE"  ] || create_datafile "$DATAFILE"

  # perform storing or restoring function
  case "$1" in
     'restore') restore  $DATAFILE ;;
     'store') store $DATAFILE ;;
     *) puke "Unknown argument";;
  esac

}

main "$@"

Getting and Setting up the Script

You can either copy the script directly or follow these steps from the command line ( to open command line , press CtrlAltT )

sudo apt-get install git
cd /opt
sudo git clone https://github.com/SergKolo/sergrep.git

The script will be located in /opt/sergrep/brightness.sh, so we do a:

sudo chmod +x /opt/sergrep/brightness.sh

to make it executable. Next, we need to set it up to work with lightdm. Create the /etc/lightdm/lightdm.conf file , either by opening it with command line nano text editor (the command is sudo nano /etc/lightdm/lightdm.conf ) or graphical gedit ( pkexec gedit /etc/lightdm/lightdm.conf )

Write to that file the following lines:

[SeatDefaults]
#display-setup-script = Script to run when starting a greeter session (runs as root)
display-setup-script = /opt/sergrep/brightness.sh restore
#display-stopped-script = Script to run after stopping the display server (runs as root)
display-stopped-script = /opt/sergrep/brightness.sh store

Save and exit

Indepth Overview

You've already discovered that you can write to /sys/class/backlight/*/brightness file directly , and you can also read in those values as well. Problem is that /sys is a virtual filesystem, so once you reboot, all the files in that filesystem disappear.

So you could store value in /sys/class/backlight/*/actual_brightness into a permanent file on every reboot. The question is how - via cron job, via lightdm, or via other means. Personally, I chose the lightdm route.

Basically we take advantage of lightdm's feature - being able to run a script before the greeter starts and after the session exits. The brightness is recorded to /opt/.last_brightness file and read from each time the script starts. We're essentially performing two actions with the same script, just by passing different arguments.

Solution 2

Have you tried this:

  1. sudo nano /etc/rc.local
  2. add this line to the file (substitute X with the desired level of brightness):

    echo X > /sys/class/backlight/intel_backlight/brightness
    
Share:
7,638

Related videos on Youtube

misha
Author by

misha

Updated on September 18, 2022

Comments

  • misha
    misha over 1 year

    The problem is that Ubuntu always resets the brightness level to maximum after every reboot. I installed the xbacklight utility, but commands like xbacklight -get or xbacklight -set XX don't work. I get nothing as the output.

    Actually, I would like to make my Ubuntu remember the level of brightness that was used last. How exactly can I do that? Here's some information:

    ls -l /sys/class/backlight/
    total 0
    lrwxrwxrwx 1 root root 0 Feb 27 09:43 radeon_bl0 -> ../../devices/pci0000:00/0000:00:01.0/drm/card0/card0-LVDS-1/radeon_bl0
    
    
    ls -l /sys/class/backlight/radeon_bl0/
    total 0
    -r--r--r-- 1 root root 4096 Feb 27 09:54 actual_brightness
    -rw-r--r-- 1 root root 4096 Feb 27 09:54 bl_power
    -rw-r--r-- 1 root root 4096 Feb 27 09:47 brightness
    lrwxrwxrwx 1 root root    0 Feb 27 09:54 device -> ../../card0-LVDS-1
    -r--r--r-- 1 root root 4096 Feb 27 09:43 max_brightness
    drwxr-xr-x 2 root root    0 Feb 27 09:54 power
    lrwxrwxrwx 1 root root    0 Feb 27 09:54 subsystem -> ../../../../../../../class/backlight
    -r--r--r-- 1 root root 4096 Feb 27 09:43 type
    -rw-r--r-- 1 root root 4096 Feb 27 09:42 uevent
    
    uname -r
    4.2.0-30-generic
    
  • misha
    misha about 8 years
    Well, I don't have "intel_backlight" in /sys/class/backlight/.
  • Jay T.
    Jay T. about 8 years
  • Fabby
    Fabby almost 7 years
    Welcome to Ask Ubuntu! :-) Please edit your answer and expand it a bit to include download and install instructions...
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 6 years
    This answer will set brightness to certain predefined level, not record and restore brightness as OP requested. This is only partial solution
  • Fabby
    Fabby over 6 years
    When you get back from holiday, ping me in chat. I get : sudo keep-brightness restore + main restore + local DATAFILE=/usr/local/bin/.last_brightness + local SYSDIR=/sys/class/backlight + '[' 1 -ne 1 ']' ++ id -u + '[' 0 -ne 0 ']' + '[' -f /usr/local/bin/.last_brightness ']' + case "$1" in + restore /usr/local/bin/.last_brightness ++ cat /sys/class/backlight/acpi_video0/max_brightness + MAX=11 + LIMIT=1 ++ cat /usr/local/bin/.last_brightness + VAL=1 + '[' 1 -lt 1 ']' + echo 1 with set -x after the comments but it doesn't restore...