I just wrote a POSIX-compliant script to set backlight (much like xbacklight but directly manipulating /sys/class/backlight/... ) that works in X and the console.
The script has an --install option that when run as root sets the permissions of the /sys/class/backlight/*/brigtness so a specified group can write to it and also to make it last over reboots installs a systemd service which sets the permissions at every system startup.
promt% backlight --help
USAGE:
backlight [ -g | --get ]
Get the current brightness value in percent of MAX brightness.
backlight { -s | --set | -i | --increase | -d | --decrease } percent
Set the brightness to 'percent' of max brightness or change it
(increase / decrease) 'percent'. 'percent' is a integer with or
without a trailing % sign. Even though -s 0 will usually give
a darker screen than -s 1, these options will never completely
turn the brightness off as it might have unexpected
consequences (see -off).
backlight { -0 | -o | --off }
Set the brightness to 0 (on some systems, 'xset dpms force off'
also sets the brightness to 0 so it may make 'xset dpms force
on' unexpectantly turn up the brightness under some
circumstances).
backlight --install [ groupname ]
Allow the specified group (or the video group if none
specified) to set the brighness forth on.
Must be run by root.
If systemd is in use 'backlight --install' will also install,
enable and run a systemd service making 'brightness' writeable
by the specified group at system startup.
'backlight --install' will replace the
/etc/systemd/system/backlightgroup.service it if it already exists.
Take a look at it on githup, where I will keep it updated if I do any changes:
https://github.com/fraxflax/backlight
Right now it just picks the first backlight-device it finds and manipulates that, if you have several folders in sys/class/backlight/ and want to manipulate a certain one you might want to replace the
SYS="/sys/class/backlight/`ls -1 /sys/class/backlight/ | head -1`"
line with something like
#SYS="/sys/class/backlight/`ls -1 /sys/class/backlight/ | head -1`"
SYS=/sys/class/backlight/acpi_video0
Here's the full script:
#!/bin/sh
#
# backlight is a POSIX-compliant (pure bourne shell) script that
# allows you to get and set the brightness of the screen via
# /sys/backlight if supported by your system.
#
# This is free software created by frax@axnet.nu,
# feel free to use, modify and/or distribute as you see fit.
#
# TODO:
# - Handle multiple backlight devices
# (currently only the first one found is used)
#
Set LOWEST to 0 to be able to completely turn the backlight off with
-set and -dec or to a higher value (e.g. 1) to prevent turning the
light completely off as this might be considered turning the screen
off and "xset dpms force on" on some systems will turn up the light
if brighness is 0.
LOWEST=1
SYS="/sys/class/backlight/ls -1 /sys/class/backlight/ | head -1"
USAGE() {
backlight=basename $0 || backlight=backlight
test -n "$1" && /bin/echo -e "\n###\n$@\n###"
cat>/dev/stderr<<EOF
USAGE:
$backlight [ -g | --get ]
Get the current brightness value in percent of MAX brightness.
$backlight { -s | --set | -i | --increase | -d | --decrease } percent
Set the brightness to 'percent' of max brightness or change it
(increase / decrease) 'percent'. 'percent' is a integer with or
without a trailing % sign. Even though -s 0 will usually give
a darker screen than -s 1, these options will never completely
turn the brightness off as it might have unexpected
consequences (see -off).
$backlight { -0 | -o | --off }
Set the brightness to 0 (on some systems, 'xset dpms force off'
also sets the brightness to 0 so it may make 'xset dpms force
on' unexpectantly turn up the brightness under some
circumstances).
$backlight --install [ groupname ]
Allow the specified group (or the video group if none
specified) to set the brighness forth on.
Must be run by root.
If systemd is in use '$backlight --install' will also install,
enable and run a systemd service making 'brightness' writeable
by the specified group at system startup.
'$backlight --install' will replace the
/etc/systemd/system/backlightgroup.service it if it already exists.
EOF
exit 1
}
[ -e "$SYS/brightness" -a -e "$SYS/max_brightness" ] || USAGE "# No supported backlight system in /sys/class/backlight"
[ "--install" = "$1" ] && {
test id -u -eq 0 || USAGE "# 'backlight -install' must be run as root"
GRP=video
test -n "$2" && GRP=$2
chgrp $GRP /sys/class/backlight//brightness ; chmod g+w /sys/class/backlight//brightness
echo
ls -l /sys/class/backlight/*/brightness
echo
test -d /etc/systemd/system && {
grep -E "^$GRP:" /etc/group >/dev/null || USAGE "# group '$GRP' not found in /etc/group"
cat>/etc/systemd/system/backlightgroup.service<<EOF
[Unit]
Description=Permission for group $GRP to set backlight brightness
After=syslog.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/chgrp $GRP /sys/class/backlight//brightness ; /bin/chmod g+w /sys/class/backlight//brightness'
RemainAfterExit=no
[Install]
WantedBy=sysinit.target
EOF
systemctl enable backlightgroup.service || USAGE "# failed to enable the backlightgroup.service"
systemctl start backlightgroup.service || USAGE "# failed to start the backlightgroup.service"
systemctl status --no-pager backlightgroup.service
}
exit 0
}
MAX=cat "$SYS/max_brightness"
CUR=cat "$SYS/brightness"
[ "-g" = "$1" -o "--get" = "$1" -o -z "$1" ] && {
echo $(($CUR*100/$MAX))%
exit 0
}
if [ "-0" = "$1" -o "-o" = "$1" -o "--off" = "$1" ]; then
NVAL=0
LOWEST=0
else
VAL=echo $2 | sed 's/[^0-9]*\([0-9][0-9]*\).*/\1/' # just keep the first found consecutive digits
ISNUM=echo $VAL | sed 's/[0-9]*//' # if there was no digits at all this will be nonempty
test -z "$VAL" -o -n "$ISNUM" && USAGE
OP=$1
case "$OP" in
-s|--set)
NVAL=$(($VAL*$MAX/100))
;;
-i|--increase)
NVAL=$(($CUR+$VAL*$MAX/100))
;;
-d|--decrease)
NVAL=$(($CUR-$VAL*$MAX/100))
;;
*)
USAGE
;;
esac
fi
test $NVAL -gt $MAX && NVAL=$MAX
test $NVAL -lt $LOWEST && NVAL=$LOWEST
echo $NVAL > "$SYS/brightness"
#echo $(($NVAL*100/$MAX))"% ($NVAL/$MAX)" >/dev/stderr
exit 0