10

Possible Duplicate:
sudo & redirect output

These are suggestions given by powertop to save energy:

echo 1500 > /proc/sys/vm/dirty_writeback_centisecs

echo 1 > /sys/module/snd_hda_intel/parameters/power_save

However, I cannot execute them from terminal even as sudo. Ideas?

0

4 Answers 4

15

Your command doesn't work because the redirection > file is done by the current shell prior to the execution of the command, so before the sudo is in effect.

There is a command, called tee, that writes to a file and to stdout what it receives on its stdin: this is handy to write something to a file without redirection. If the file can be modified only by root, it is sufficient to prepend sudo to tee.

echo 1500 | sudo tee /proc/sys/vm/dirty_writeback_centisecs

Another way to obtain the desired result, mantaining the redirection, is to move the redirection to a subshell executed by root, through sudo:

sudo sh -c 'echo 1500 > /proc/sys/vm/dirty_writeback_centisecs'

Lastly, you can enter a root shell in various ways, and you remain root until you exit explicitly that shell:

sudo su
sudo -s
sudo bash

For more information see the manuale pages of sudo, su and of course of bash.

3
  • I always forget about tee :-X +1 tomorrow ;) and dago: accept this one please instead of mine. it is shorter and a better answer.
    – Rinzwind
    Jun 23, 2011 at 18:52
  • Gentlem, as a beginner thanks but still a load of questions. I know hoe to make a script executable but what is the difference to do sudo su before and how? wHAT is the sudo tee or sudo sh - c
    – dago
    Jun 23, 2011 at 19:45
  • I will give more information in the answer
    – enzotib
    Jun 23, 2011 at 19:47
9

Instead of creating a cronjob for setting values as described by @Seppo Erviälä, you can make use of the configuration files of procps and sysfsutils.

/proc/sys

Values for /proc/sys can be set in the file /etc/sysctl.conf. To make echo 1500 > /proc/sys/vm/dirty_writeback_centisecs persistent, add a line with:

vm.dirty_writeback_centisecs = 1500

To load the changes in /etc/sysctl.conf to the current session, run:

sudo sysctl -p

/sys

For setting values in /sys the package sysfsutils needs to be installed. The configuration file is located at /etc/sysfs.conf. To make echo 1 > /sys/module/snd_hda_intel/parameters/power_save persistent, add a line with:

module/snd_hda_intel/parameters/power_save = 1

To apply the values in /etc/sysfs.conf to the current session, run:

sudo /etc/init.d/sysfsutils restart
1
  • Thanks for info, this seems much more proper way to do this Jun 24, 2011 at 11:11
2

Do a sudo -s or a sudo su first:

enter image description here

From a script:

# more powersave
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
echo 1 > /sys/module/snd_hda_intel/parameters/power_save
# ./powersave 
# 

Best practice would be to ussue the sudo -s before activating the script (hence the #).

5
  • Thanks, of course you earned a voting up and solved, however, how can I use this in a script together with other power saving measures? BTW what is the difference between sudo su and command and sudo command?
    – dago
    Jun 23, 2011 at 18:34
  • Toss the lines in a script, make it executable and run sudo su before activating the script.
    – Rinzwind
    Jun 23, 2011 at 18:40
  • ... or run each line with "sudo -i " immediately before the echo statements
    – fossfreedom
    Jun 23, 2011 at 18:41
  • worked on my test program - also see @enzotib answer for an alternate
    – fossfreedom
    Jun 23, 2011 at 18:51
  • @Rinzwind Using a root shell is not a "best practice" in Ubuntu. To allow writing to privileged locations using redirection operators, see askubuntu.com/q/20578/6969
    – Lekensteyn
    Dec 18, 2011 at 9:07
2

EDIT: See answer by @Lekensteyn for more proper way to edit /proc/sys and /sys defaults.

Running these suggestions from command line will only enable them for your current session and they will reset to defaults after a reboot. If you want to enable these suggestions each time your system starts you should make them into a script:

#!/bin/dash
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
echo 1 > /sys/module/snd_hda_intel/parameters/power_save

You can place this script somewhere convinient eg. /root/power_save.sh.

Then you want to make sure it runs with root privileges each time your system starts. This can be done with sudo crontab -e which opens list of time based tasks for root. Add line:

@reboot /root/power_save.sh

Don't forget to make your script executable:

sudo chmod u+x /root/power_save.sh

This way these power saving options will be enabled for all users, even before login, and no password is needed to authorize them each time.

Not the answer you're looking for? Browse other questions tagged .