5

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
6
+25

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: 1047481448@qq.com 
# 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.

  • 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... – Fabby Jan 2 '18 at 20:44
1

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
    
  • 1
    Well, I don't have "intel_backlight" in /sys/class/backlight/. – misha Mar 4 '16 at 13:32
  • Try this: askubuntu.com/questions/476664/… – Jay T. Mar 4 '16 at 13:42
  • This answer will set brightness to certain predefined level, not record and restore brightness as OP requested. This is only partial solution – Sergiy Kolodyazhnyy Dec 5 '17 at 2:52
0

If you are able to set the brightness with a command, making the brightness level change to a particular value at startup is easy. Do the following:

  • Open Dash by pressing Super/Windows key
  • Enter "Startup Applications" and press enter
  • Click "Add"
  • Enter a name (irrelevant) and the command
  • Click "Add"

This should execute that command on startup. I am unsure of how to make it remember the previous brightness, but I hope this helps.

0

Alright. I'm going to answer my own question just for future reference. In my case, what I did is add the following lines (it's really just one line with a bunch of comments to remind me why and what I did there) to /etc/rc.local before exit 0:

# The following line should solve the problem of Ubuntu resetting
# the brightness level back to maximum after every reboot.

echo 50 > /sys/class/backlight/radeon_bl0/brightness

This is what the entire file looks like now:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# The following line should solve the problem of Ubuntu resetting
# the brightness level back to maximum after every reboot.
echo 50 > /sys/class/backlight/radeon_bl0/brightness

exit 0

Although I'm not one hundred percent sure if this is the best way to do it, everything seems to be working just fine for me now.

0

Just use light instead... Otherwise it is a mess to fix it, especially if you have intel and nvidia graphics together!

This Works on Ubuntu 16.04... and is tested on Alienware M14XR2

Checkout->https://github.com/haikarainen/light

  • 2
    Welcome to Ask Ubuntu! :-) Please edit your answer and expand it a bit to include download and install instructions... – Fabby Jun 15 '17 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.