How do I add more than one command to /etc/rc.local?

10,902

It turned out to be a timing issue. The OP indicated fixing it by delaying the execution of the first command thus:

(sleep 5; echo 3021 > /sys/class/backlight/intel_backlight/brightness)&

(Huckle had suggested this in comments.)

Share:
10,902

Related videos on Youtube

Kyle Macey
Author by

Kyle Macey

I'm a Ruby developer and GitHubber in Upstate New York.

Updated on September 18, 2022

Comments

  • Kyle Macey
    Kyle Macey over 1 year

    I want to add two power saving commands to /etc/rc.local file.

    This to disable Bluetooth:

     rfkill block bluetooth
    

    And this to reduce screen brightness:

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

    Separately added to /etc/rc.local they work but not both of them together like this:

    #/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.
    
    echo 3024 > /sys/class/backlight/intel_backlight/brightness
    
    rfkill block bluetooth
    
    exit 0
    

    How do I add the two commands to get them properly executed at start-up?

    Update

    It turned out to be a timing issue. I fixed it by delaying the execution of the first command thus:

    (sleep 5; echo 3021 > /sys/class/backlight/intel_backlight/brightness)&
    
    • Paul Hänsch
      Paul Hänsch over 11 years
      This should definitely work. My suspicion is, that the first command terminates with an error, although I cannot figure out why. Since the Parameter -e is given in the very first line of the file, the shell script would terminate after the very first error without executing any more commands.
    • Admin
      Admin over 11 years
      Tried again as above and it seems to be the brightness command that's the problem. Bluetooth is consistently killed on start up but the brightness is strangely only sometimes reduced. Experimented with only the brightness command in the file with the result that, regardless of how I shut down, in less than 50% it works. Note that with both the commands in the file, as above, rfkill always works so the command doesn't terminate with an error, brightness just doesn't always take effect. Is there another place I could put 'echo 3024 > /sys/class/backlight/intel_backlight/brightness'?
    • Paul Hänsch
      Paul Hänsch over 11 years
      yes, you could put in into the crontab for root. Run sudo crontab -e and insert a line @REBOOT echo 3024 > /sys/class/backlight/intel_backlight/brightness. You have probably a timing problem in the form that the driver controlling display brightness is sometimes not yet loaded when the script comes up. You might or might not avoid this when using the crontab. The crontab can hold several lines, each user has its own crontab.
    • Admin
      Admin over 11 years
      Thanks, but when I tried that I got 'no crontab for root - using an empty one crontab: installing new crontab "/tmp/crontab.ytX5oW/crontab":22: bad time specifier errors in crontab file, can't install. Do you want to retry the same edit? (y/n)' - how do I save the changes properly?
    • Paul Hänsch
      Paul Hänsch over 11 years
      whoops, my mistake, spell it @reboot ... small letters. Running man 5 crontab shows the documentation for this file format
    • Admin
      Admin over 11 years
      Got the crontab installed but it doesn't work. Think you're right that it's a timing problem - seems like the brightness briefly flickers just before the login screen.
    • Huckle
      Huckle over 11 years
      It's a dirty hack but why not do something like (sleep 5; echo 3021 > /sys/class/backlight/intel_backlight/brightness)&
    • Huckle
      Huckle over 11 years
      A less hacky variant might use udev if that is available for your backlight. This would be best since it avoids the race condition entirely.
    • Admin
      Admin over 11 years
      Do you mean adding (sleep 5; echo 3021 > /sys/class/backlight/intel_backlight/brightness)& to /etc/rc.local?
    • isaaclw
      isaaclw over 11 years
      You should add a note to your question, or answer it below, for those that don't want to read the comments. ;)