1

I want to run this script on ubuntu 19.04, after booting the OS to restore the previous screen brightness:

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

How can I run this script so it takes effect at the right moment?

3 Answers3

2

In Ubuntu 20.04.2 LTS I solved this problem by creating a cron job:

sudo crontab -e

and including this line at the end of the file:

@reboot (sleep 2 ; echo 80 > /sys/class/backlight/nvidia_0/brightness) &

I found that 2 seconds was enough time to override Nvidia's reset timing at boot. You can adjust the brightness level (80) to your own preference.

PS: Just found out that an additional fix is needed: even though the code above fixes brightness at boot, it won't do it when the computer wakes up from sleep/hibernation, Nvidia resets brightness to max again. To fix this, create in addition the following script in a file in the /lib/systemd/system-sleep/ folder:

#!/usr/bin/env bash
 action="$1"
 case $1 in
   post)
     echo 80 > /sys/class/backlight/nvidia_0/brightness
     ;;
 esac
0

Put it in a script eg brightness.sh. , with #!/bin/bash at the top (not sure if this is necessary) make it executable (right click, permissions, executable) , then put that in startup applications - Power Key then type startup, select Startup applications, add button, type (<> are your particular locations)

bash "/home/<username>/<MySctipts>/brightness.sh"
pierrely
  • 653
0

Your system may be setup to automatically restore the last brightness setting already. You can however edit /etc/rc.local and insert this line before exit 0 at the bottom:

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

Note you can also use cron@reboot if rc.local-service isn't setup in systemd:

  • Ubuntu 19.04 doesn't use rc.local also I have tried creating the file properly with exit 0 and all of that and doesn't work. https://askubuntu.com/questions/886620/how-can-i-execute-command-on-startup-rc-local-alternative-on-ubuntu-16-10 – Smeterlink Aug 19 '19 at 16:16
  • Try the second answer which sets up service file as described in my link. Cron@reboot is another option and you might find that a better fit. – WinEunuuchs2Unix Aug 19 '19 at 17:12
  • I need to run the command AFTER the reboot not before, are you sure cron@reboot does that properly? How do I add the line? – Smeterlink Aug 19 '19 at 18:23