Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm following advice from LessWatts to lower battery drain with the following commands:

mount -o remount,noatime /
hdparm -B 1 -S 12 /dev/sda
echo 3000 > /proc/sys/vm/dirty_writeback_centisecs
iwconfig wlan0 power on
echo 10 > /sys/module/snd_hda_intel/parameters/power_save

The question is, how do I set them up to start automatically with system start?

share|improve this question
    
Put that in a scrip, make executable, copy to /etc/pm/power.d/ and /etc/pm/sleep.d/. I don't think the fourth command helps with power saving in any way. –  mikewhatever Feb 26 '13 at 16:49

1 Answer 1

up vote 7 down vote accepted

I believe you can use the /etc/rc.local for a root boot script.

First copy all the commands into a text file, then append this line at the top of the file:

#!/bin/bash

then save it somewhere, perhaps your home dir. ( /home/user/battery-script.sh )

Then make it executable, in terminal:

$ chmod +x /home/user/battery-script.sh

Now in terminal:

$ gksudo gedit /etc/rc.local

Now before the line exit 0 write the path to your script. ( /home/user/battery-script.sh ) Make sure exit 0 is after everything else, or else it will exit before executing the commands.

Now just to be sure of when the script is run, you will probably want to add an xmessage command so that a message pops up when it is run.

xmessage -center "Battery script run in /etc/rc.local"

make sure it's before the exit 0.

Now, reboot to test to make sure it runs on startup, then you can remove the xmessage line if it is satisfactory.

If this does not work for some reason, you can fall back to GNOME's autostart manager. It involves editing the sudoers file to allow root access to your script without a password. We will edit the file's permissions and ownership as to not create a security hole.

First, move the entire script into the /bin/ folder:

$ sudo mv battery-script.sh /bin/

set root ownership and permissions preventing a regular user from inserting malicious code to be run as root:

$ sudo chown root /bin/battery-script.sh
$ sudo chgrp root /bin/battery-script.sh
$ sudo chmod 555 /bin/battery-script.sh

Now that's set, time to edit the sudoers file:

$ gksudo gedit /etc/sudoers

Go to the bottom and add this line. Replace $USER with your username

$USER    ALL = NOPASSWD:/bin/battery-script.sh

Now you can run the script without a password when using sudo.

Next, open Startup Applications, click add. Put anything under Name, and under command put:

sudo /bin/battery-script.sh

That's it! Your script will run as root when you boot up, you can add a line in the battery-script.sh echo > /home/user/it_works.txt (replace with username) and that file will be created upon the next boot so you can be sure it's working, or as mentioned earlier, you can use xmessage.

share|improve this answer
    
could you put that on pastebin.com instead so the formatting shows correctly? –  Matt Feb 26 '13 at 17:37
    
sure, pastebin.com/URgS56d7 –  Venem Feb 26 '13 at 17:38
    
ah ok, sorry I use "[]" as parenthesis, so I did not mean to put them in the script. Now the question is do you get an xmessage alert when you reboot? If no, check it manually in the terminal to make sure it's installed. If yes, all you need to do is remove the brackets on the rc.local script. –  Matt Feb 26 '13 at 17:43
    
basically type xmessage test to see if xmessage is installed. I thought it was installed by default, just making sure. –  Matt Feb 26 '13 at 18:00
    
Also what Ubuntu version do you have? if you don't know do lsb_release -r in terminal –  Matt Feb 26 '13 at 18:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.