2

I have been trying to get my laptop's screen brightness to default to a low setting, and for my keyboard light to be off when I start my computer.

I have these two commands that work when I execute them at the command line:

xbacklight -set 7
echo 0 | sudo tee /sys/class/leds/asus::kbd_backlight/brightness

I put them in my /etc/rc.local file, like so:

#!/bin/sh -e
# ...
# By default this script does nothing.
echo 0 | tee /sys/class/leds/asus::kbd_backlight/brightness
xbacklight -set 7
exit 0

However, neither command executes. (I was told that the sudo was not needed in the second command when placed in /etc/rc.local)

There was another question asking the same thing about rc.local, but I tried to also include my commands using the Startup Applications GUI interface. That also did not work, so I think the problem might go beyond just the rc.local file. Other default startup commands in the list seem to start, so far as I can tell.

Why am I unable to get any custom command line to execute when I startup?


New: After some experimentation, it seems like the commands I put in /etc/rc.local might be getting executed before the login screen. So, my login screen has the screen appropriately dimmed. However, then when I login, my screen goes to maximum brightness. So it seems like perhaps there is some other setting that is being applied when I log in, or that /etc/rc.local only applies for the login screen itself and has no bearing on what happens to a user when logged in.

So, how do I resolve this so that the brightness and keyboard light settings I want apply to the user login session, not just to the login screen?

Questioner
  • 6,729
  • 33
  • 106
  • 165
  • 1
    Does manually executing this script work? – nitishch Feb 05 '14 at 10:12
  • @nitish, thanks for asking. I just tested now, and yes, it works. If I run it from the command line, it asks for my administrative password, and then when I enter that, it executes. – Questioner Feb 06 '14 at 02:50
  • what is the output of `ls /sys/class/backlight/` – Raja G Feb 08 '14 at 05:43
  • The fact that `xbacklight` is not working when put in `/etc/rc.local` seems easy to explain... The X server has not started yet when the system executes this file. On the other hand, it is really strange that it is not working in your startup session option... – Rmano Feb 08 '14 at 05:57
  • 3
    How about when you put them in your `.bashrc` file in your home directory? – Parto Feb 08 '14 at 05:58
  • @AvatarParto: Thank you for the `.bashrc` suggestion. However, the keyboard command seems to require root permission, and unless I'm mistaken, `.bashrc` does not have root permission, only the logged in user. There must be a way to resolve that, though. After all, I can lower and turn off the keyboard backlight with a straight keyboard command, so it shouldn't be something that can only be done by root...? – Questioner Feb 08 '14 at 06:37
  • @DaveMG output of ls /sys/class/backlight/ please – Raja G Feb 08 '14 at 07:53
  • @rajagenupula: `ls /sys/class/backlight/ acpi_video0 intel_backlight` – Questioner Feb 08 '14 at 08:22
  • @AvatarParto: I've experimented with `.bashrc`, and it does not execute on login, it only executes when I open a terminal window. That is not as automatic as I would like it to be. – Questioner Feb 08 '14 at 09:12
  • How about adding a startup command in the form of a .desktop file under /etc/xdg/autostart. – Sadi Feb 08 '14 at 11:14
  • @Sadi,thank you for the suggestion. I have looked at other `.desktop` files to see how it works, but the format is not clear. Is there anywhere that might instruct me on how to use these particular commands within a `.desktop` file? – Questioner Feb 09 '14 at 04:56
  • You can put your executable script file to a location such as `/usr/local/bin/lower-brightness` and then enter this file in a new .desktop file under `/etc/xdg/autostart/` like `Exec=/usr/local/bin/lower-brightness` together with other settings such as `X-GNOME-Autostart-enabled=true` and perhaps `NoDisplay=false` and/or `Hidden=false`. I hope this works, otherwise, I might have another suggestion ;-) – Sadi Feb 09 '14 at 09:28
  • @Sadi, thanks for that. I created a `.desktop` file, and unfortunately, the problem persists, but now I think it might be different than what I thought. I've [opened another questions detailing the issue](http://askubuntu.com/q/418421/17041). – Questioner Feb 09 '14 at 11:53
  • Have you tried the **xbacklight solution** I've suggested as a separate answer? I tested and saw that it worked on my machine. – Sadi Feb 10 '14 at 09:45
  • Back to the `.bashrc` (or `.profile`) suggestion. If I got that right, the password was the only issue there. If so, you could define this command for your user not requiring a sudo password (via a corresponding line in `/etc/sudoers.d/user-alias`, e.g. `dave ALL = NOPASSWD: /home/dave/startup.sh`, and then execute `sudo /home/dave/startup.sh` from your `.bashrc`/`.profile`. – Izzy Feb 11 '14 at 23:33
  • @Izzy, thank you for the suggestion. I tried setting up the script file and .profile as you suggested, but it does not seem to be executing. Does `.profile` definitely execute at every startup? Also, I had to change `sudo /home/dave/startup.sh` to `sudo sh /home/dave/startup.sh` because otherwise when I tested it at the command line it said `command not found`. – Questioner Feb 12 '14 at 06:09
  • `.profile` is executed at login, `.bashrc` whenever you start a new bash shell (and there's also the `.xprofile`, executed on start of the X-Window session). And you might have needed to add the `sh` because you didn't make `startup.sh` executable (`chmod +x startup.sh`). – Izzy Feb 12 '14 at 09:20

5 Answers5

4

xbacklight solution

1) Create an executable script file e.g. /home/YOU/.bin/lower-brightness like this:

#!/bin/sh
xbacklight -set 7 &

2) Create a .desktop file e.g. /home/YOU/.config/autostart/lower-brightness.desktop like this:

[Desktop Entry]
Type=Application
Exec=/home/YOU/.bin/lower-brightness
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Lower Screen Brightness
Comment=Screen brightness is set to 7 at startup
Icon=display
Sadi
  • 10,912
  • 7
  • 48
  • 61
1

To have the display settings as you want you can add this line in /etc/rc.local

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

Then it can keep your brightness as 7.

hope that helps a bit.

Raja G
  • 100,643
  • 105
  • 254
  • 328
  • Thank you for responding, but you seem to have missed the point of the question. It's not that I don't have the right commands, it's that the commands are not executing. – Questioner Feb 09 '14 at 04:54
  • Have you tried my solution? – Raja G Feb 09 '14 at 05:58
  • Yes, I tried your suggestion. Unfortunately, it had no different effect than any of the others offered. It may be due to a new dimension to the problem, which is one of competing commands. I have opened [a new question](http://askubuntu.com/q/418421/17041) to address this. – Questioner Feb 09 '14 at 08:40
1

I'm sure, my solution should help you if you use ubuntu with lightdm.

I was searching for turning on NumPad on my laptop when it starts and in lightdm documentation I found this:

# display-setup-script = Script to run when starting a greeter session (runs as root)
# greeter-setup-script = Script to run when starting a greeter (runs as root)
# session-setup-script = Script to run when starting a user session (runs as root)
# session-cleanup-script = Script to run when quitting a user session (runs as root)

And that is solution. You need to create file in /usr/bin/, say /usr/bin/backlight and write commands there.

#!/bin/bash
xbacklight -set 7
echo 0 | sudo tee /sys/class/leds/asus::kbd_backlight/brightness
exit 0

(Also xbacklight doesn't work for me. echo 7 > /sys/class/backlight/intel_backlight/brightness does the stuff). Than make sure you add execute permission for this file with chmod a+x /usr/bin/backlight.

Than you need to edit /etc/lightdm/lightdm.conf and write for example this line:

session-setup-script=/usr/bin/backlight

And that's it. Now restart you PC.

Viktor K
  • 1,139
  • 1
  • 10
  • 17
1

Create your own init script to adjust the brightness levels.

echo '#!/bin/sh 
sleep 60
echo 0 | tee /sys/class/leds/asus::kbd_backlight/brightness
xbacklight -set 7
exit 0' > /tmp/myinit
sudo mv /tmp/myinit /etc/init.d/myinit
sudo chmod +x /etc/init.d/myinit
sudo update-rc.d myinit defaults  

Adjust the sleep value to your suit.

totti
  • 6,768
  • 4
  • 38
  • 48
0

I wonder why no one has suggested using update-rc.d. I would not put the script into /etc/rc.local manually. I would recommend this way to make programs run at startup:

sudo cp lower-brightness.sh /etc/init.d/
sudo chmod +x /etc/init.d/lower-brightness.sh 
sudo update-rc.d lower-brightness.sh defaults 

This will make sure the script is linked to appropriate run levels.

biocyberman
  • 778
  • 6
  • 20
  • Thank you for this suggestion. When I tried your solution, after logging in it seemed like my desired settings had applied. Unfortunately, though, as soon as I moved my pointer by using the laptop's touchpad, the screen brightness changed and the keyboard light came on again. I am extremely perplexed by this. – Questioner Feb 15 '14 at 06:05
  • That means your setting `xbacklight -set 7` is not maintained after boot. Try changing to this `xbacklight -set 7 &` in your statup script to have the command run in background. – biocyberman Feb 17 '14 at 13:05
  • I added the `&` as you suggested, but that didn't change anything. I also tried removing the `xbacklight` command entirely so as to just get the keyboard to work, but when I did that, the keyboard light never shuts off at all. – Questioner Feb 18 '14 at 07:55