run sudo /usr/sbin/powertop --auto-tune after login

Hello folks,
As the title suggests, I need to run

sudo /usr/sbin/powertop --auto-tune 

after login into KDE or after/during the boot process (I’m not well versed with init levels. So, excuse me). I’m tired of supplying admin password after every login, so I want to get it over with, once and for all. Below are the things I’ve tried so far and they didn’t give desired result.

  1. create a startup script and launch it with KDE auto start. (Didn’t work because it needs root password.)
  2. tinker with /etc/init.d/after.local and /etc/init.d/boot.local (This didn’t work, I could confirm this by running sudo /usr/sbin/powertop --html)
  3. create a service /etc/systemd/system/powertop.service (This didn’t work either as the powertop suggestion are not applied),contents of /battery_optimisation.sh (oh, and not to forget, this is executable) and
sudo systemctl enable powertop.service

I may be missing something obvious or totally heading in a wrong direction here. So, please tell me how to achieve this

  1. run
sudo /usr/sbin/powertop --auto-tune 

without asking me for root password everytime
or
2. run the script(executable) powertop suggestions on every boot without asking me for root password.

OS: OpenSuse 13.1 64-bit
KDE 4.14.0

Thanks in advance,
Hrudai

To begin with, when you try to do this from any init/systemd script at boot, the command should of course not be sudo. Those things are run as root already.

Oh, yeah, thanks for reminding that. But I didn’t use sudo in init/systemd script(s).

In the .service file you call /battery_optimisation, but the script apparently is named /battery_optimisation.sh.
Maybe that’s the reason why it doesn’t work?

Oh, and “sudo systemctl status powertop.service” should hint at that or other problems…:wink:

You could have avoided my comment by showing what you have in that file instead of suggesting. Copy/paste the prompt, the command (a cat statemrnt in this case I presume), the output and the next prompt. The we can all see exactly what you have without you having to add more story telling then needed.
:wink:

I’ve tried it with and without extension but no luck

powertop.service                      loaded inactive dead    Battery Opstimistaion Script

that didn’t say much.

Thanks,
Hrudai.

Ok.
But have you tried to start it manually to see if the .service works?

sudo systemctl start powertop.service
powertop.service                      loaded inactive dead    Battery Opstimistaion Script

that didn’t say much.

Right. But what command gave that output?
That doesn’t look like the output of “systemctl status” to me. Rather “systemctl --list-units”.

Please post the output of “sudo systemctl status powertop.service” as I wrote.

Also, did “systemctl enable” give any error message?

Oh, sorry. I’ve actually hyperlinked to susepaste in my original post ;). But anyway here’s the file contents.

#! /bin/sh
#
# Copyright (c) 2010 SuSE LINUX Products GmbH, Germany.  All rights reserved.
#
# Author: Werner Fink, 2010
#
# /etc/init.d/after.local
#
# script with local commands to be executed from init after all scripts
# of a runlevel have been executed.
#
# Here you should add things, that should happen directly after
# runlevel has been reached.  Common environment
# variables for this are:
#  RUNLEVEL  -- The current system runlevel.
#  PREVLEVEL -- The previous runlevel (useful after a runlevel switch).
#
/battery_optimisation
/usr/sbin/powertop --auto-tune
exit 0


#!/bin/bash
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.
#
# Author: Werner Fink, 1996
#         Burchard Steinbild, 1996
#
# /etc/init.d/boot.local
#
# script with local commands to be executed from init on system startup
#
# Here you should add things, that should happen directly after booting
# before we're going to the first run level.
#
/battery_optimisation.sh
/usr/sbin/powertop --auto-tune
exit 0


contents of powertop.service


[Unit]
Description=Battery Opstimistaion Script

[Service]
ExecStart=/battery_optimisation

[Install]
WantedBy=default.target

Thanks,
Hrudai.

There are no extentsions in Linux. When the name of the file is /battery_optimisation.sh you should always use that, all the characters.

yeah, you’re right.


sudo systemctl status powertop.service

powertop.service - Battery Opstimistaion Script
   Loaded: loaded (/etc/systemd/system/powertop.service; enabled)
   Active: inactive (dead) since Mon 2014-10-13 18:40:49 IST; 1h 56min ago
  Process: 1058 ExecStart=/battery_optimisation (code=exited, status=0/SUCCESS)
 Main PID: 1058 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/powertop.service

Oct 13 18:40:49 Anon systemd[1]: Starting Battery Opstimistaion Script...
Oct 13 18:40:49 Anon systemd[1]: Started Battery Opstimistaion Script.



sudo systemctl enable powertop.service
ln -s '/etc/systemd/system/powertop.service' '/etc/systemd/system/default.target.wants/powertop.service'

there were no explicit errors that I took note of.

oopse, my bad. So,I edited the file name before my last boot. It’s “battery_optmisation” not “battery_optimisation.sh”

Thanks,
Hrudai.

So it has been started.
As it is no daemon that keeps running in the background (it’s just a simple shell script after all that sets some values and exits), it is “inactive (dead)” now.

So have you verified that starting it manually (“sudo systemctl start powertop.service”) does set those things as you want to?

Probably it gets started too soon then? systemd tries to start every service as soon as possible.
Try to add some “After=” to the .service file, maybe “After=localfs.service”.

PS: to start your script as root without having to enter the root password, you might make it owned by root and set the “s” permission bit.

sudo chmod +s /battery_optimisation

You can then run it as user without sudo.

Yes, it did work.

Probably it gets started too soon then? systemd tries to start every service as soon as possible.
Try to add some “After=” to the .service file, maybe “After=localfs.service”.

I edited powertop.service. Now, this is how it looks like.


[Unit]
Description=Battery Optimisation Script
After=localfs.service

[Service]
ExecStart=/battery_optimisation

[Install]
WantedBy=default.target

But this didn’t work in start up, it worked when I invoked the service manually.

PS: to start your script as root without having to enter the root password, you might make it owned by root and set the “s” permission bit.

sudo chmod +s /battery_optimisation

You can then run it as user without sudo.

ls -l batt*
-rwxrwxrwx 1 hrudai users 1016 Oct 13 21:47 battery_optimisation

hrudai@Anon:/> ./battery_optimisation
./battery_optimisation: line 2: /proc/sys/vm/dirty_writeback_centisecs: Permission denied
./battery_optimisation: line 3: /sys/class/scsi_host/host0/link_power_management_policy: Permission denied
./battery_optimisation: line 4: /sys/class/scsi_host/host1/link_power_management_policy: Permission denied
./battery_optimisation: line 5: /sys/class/scsi_host/host2/link_power_management_policy: Permission denied
./battery_optimisation: line 6: /sys/class/scsi_host/host3/link_power_management_policy: Permission denied
./battery_optimisation: line 7: /sys/class/scsi_host/host4/link_power_management_policy: Permission denied
./battery_optimisation: line 8: /sys/class/scsi_host/host5/link_power_management_policy: Permission denied
./battery_optimisation: line 9: /sys/module/snd_hda_intel/parameters/power_save: Permission denied
./battery_optimisation: line 10: /proc/sys/kernel/nmi_watchdog: Permission denied
./battery_optimisation: line 11: /sys/bus/usb/devices/1-1/power/control: Permission denied
./battery_optimisation: line 12: /sys/bus/usb/devices/2-1/power/control: Permission denied
./battery_optimisation: line 13: /sys/bus/usb/devices/usb1/power/control: Permission denied
./battery_optimisation: line 14: /sys/bus/usb/devices/usb2/power/control: Permission denied
./battery_optimisation: line 15: /sys/bus/usb/devices/1-1.6/power/control: Permission denied


Thanks,
Hrudai.

What do you mean with “this didn’t work”?
Was the script not started, or were the values not set?
I.e. what did “systemctl status” say?

Maybe use “After=xdm.service”, that should be late enough I guess (but wouldn’t work if you boot to text-mode of course).

ls -l batt*
-rwxrwxrwx 1 hrudai users 1016 Oct 13 21:47 battery_optimisation

hrudai@Anon:/> ./battery_optimisation
./battery_optimisation: line 2: /proc/sys/vm/dirty_writeback_centisecs: Permission denied
./battery_optimisation: line 3: /sys/class/scsi_host/host0/link_power_management_policy: Permission denied
./battery_optimisation: line 4: /sys/class/scsi_host/host1/link_power_management_policy: Permission denied
./battery_optimisation: line 5: /sys/class/scsi_host/host2/link_power_management_policy: Permission denied
./battery_optimisation: line 6: /sys/class/scsi_host/host3/link_power_management_policy: Permission denied
./battery_optimisation: line 7: /sys/class/scsi_host/host4/link_power_management_policy: Permission denied
./battery_optimisation: line 8: /sys/class/scsi_host/host5/link_power_management_policy: Permission denied
./battery_optimisation: line 9: /sys/module/snd_hda_intel/parameters/power_save: Permission denied
./battery_optimisation: line 10: /proc/sys/kernel/nmi_watchdog: Permission denied
./battery_optimisation: line 11: /sys/bus/usb/devices/1-1/power/control: Permission denied
./battery_optimisation: line 12: /sys/bus/usb/devices/2-1/power/control: Permission denied
./battery_optimisation: line 13: /sys/bus/usb/devices/usb1/power/control: Permission denied
./battery_optimisation: line 14: /sys/bus/usb/devices/usb2/power/control: Permission denied
./battery_optimisation: line 15: /sys/bus/usb/devices/1-1.6/power/control: Permission denied

Of course you cannot run the script as user because you have insufficient permissions to set those values.

I said you should make the script owned by root and set the SUID bit.
But forget about that, it doesn’t work with scripts anyway.

You could add a line like this to /etc/sudoers (use “visudo” to edit it) to be able to run it with sudo without having to enter a password:

username ALL=(ALL) NOPASSWD: /battery_optimisation

Or if you prefer, create a file with that content in /etc/sudoers.d/.

Yep SUID bit is not allowed on scripts, the Linux kernel does not allow it because it is exploitable and yes in an init script the default should be root user unless specified other wise.

Finally, I got this thing to do what I wanted to.

  1. Added powertop to sudoers
  2. Invoke powertop via a script that runs with kde auto-start.

Thank you very much for all your inputs.

-Hrudai.