让Ubuntu记住没有xbacklight的屏幕亮度级别

问题是Ubuntu总是在每次重启后将亮度级别重置为最大值。 我安装了xbacklight实用程序,但xbacklight -getxbacklight -set XX类的命令不起作用。 我没有得到任何输出。

实际上,我想让我的Ubuntu记住最后使用的亮度级别。 我怎么能这样做? 以下是一些信息:

 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 

实际上,我想让我的Ubuntu记住最后使用的亮度级别。 我怎么能这样做? 以下是一些信息:

介绍

下面的脚本解决了OP需要存储和恢复上次使用的屏幕亮度的问题。 它与the lightdm greeter配合使用the lightdm并由lightdm激活。 没有要求使用lightdm ,所以如果你喜欢使用cron作业,你可以这样做。

基本理念:

  1. 将脚本存储在某处(通过在此处或通过github获取)
  2. 使用sudo权限创建/etc/lightdm/lightdm.conf
  3. 确保该文件有3行: [SeatDefaults]display-setup-scriptdisplay-stopped script 。 请看下面的详细信息。 脚本标题也提供了概述。

脚本来源

 #!/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 "$@" 

获取和设置脚本

您可以直接复制脚本,也可以从命令行执行以下步骤(要打开命令行,请按Ctrl Alt T

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

该脚本将位于/opt/sergrep/brightness.sh ,因此我们执行以下操作:

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

使其可执行。 接下来,我们需要将其设置为与lightdm一起使用。 创建/etc/lightdm/lightdm.conf文件,方法是使用命令行nano文本编辑器(命令为sudo nano /etc/lightdm/lightdm.conf )或图形geditpkexec gedit /etc/lightdm/lightdm.conf )打开它pkexec gedit /etc/lightdm/lightdm.conf

写入该文件的行如下:

 [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 

保存并退出

深入概述

您已经发现可以直接写入/sys/class/backlight/*/brightness文件,也可以读入这些值。 问题是/sys是一个虚拟文件系统,所以一旦你重新启动,该文件系统中的所有文件都会消失。

因此,您可以在每次重新启动时将/sys/class/backlight/*/actual_brightness值存储到永久文件中。 问题是如何 – 通过cron作业,通过lightdm,或通过其他方式。 就个人而言,我选择了lightdm路线。

基本上我们利用lightdm的function – 能够在欢迎开始之前和会话退出之后运行脚本。 亮度记录到/opt/.last_brightness文件中,并在每次脚本启动时读取。 我们实际上是使用相同的脚本执行两个操作,只需传递不同的参数即可。

你试过这个:

  1. sudo nano /etc/rc.local
  2. 将此行添加到文件中(用所需的亮度级别替换X):

     echo X > /sys/class/backlight/intel_backlight/brightness 

如果您能够使用命令设置亮度,则在启动时将亮度级别更改为特定值很容易。 请执行下列操作:

  • 按Super / Windows键打开Dash
  • 输入“Startup Applications”并按Enter键
  • 点击“添加”
  • 输入名称(不相关)和命令
  • 点击“添加”

这应该在启动时执行该命令。 我不确定如何让它记住以前的亮度,但我希望这会有所帮助。

好的。 我将回答我自己的问题,以备将来参考。 在我的情况下,我做的是在exit 0之前添加以下行(它实际上只有一行带有一堆注释以提醒我为什么以及我在那里做了什么)到/etc/rc.local

 # 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 

这就是整个文件现在的样子:

 #!/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 

虽然我不是百分之百确定如果这是最好的方法,现在一切似乎对我来说都很好。

只需使用灯光……否则修复它会很麻烦,特别是如果你有英特尔和nvidia图形在一起!

这适用于Ubuntu 16.04 ……并在Alienware M14XR2上进行了测试

结帐 – > https://github.com/haikarainen/light