Hjdskes / powersave

Linux power save settings, compatible with systemd
Do What The F*ck You Want To Public License
137 stars 21 forks source link

Check udev rule at startup #1

Closed orschiro closed 11 years ago

orschiro commented 11 years ago

Hi Unia,

I was inspired by your udev rule to check whether my laptop is on AC or not. However, in case I was on battery, turned off the system and plugged in the charging cable, after restart the udev rules is not run and my laptop remains with the settings for battery mode.

How can I get the udev rule checked on boot?

Regards,

Robert

Hjdskes commented 11 years ago

Hm, I have not experienced this behaviour on my own laptop. First, let's find out some details that will help troubleshooting:

  1. What distro are you using?
  2. Are you using systemd, if not, what are you using?
  3. Does it work as expected when the computer is on?
orschiro commented 11 years ago

Hi,

  1. Arch
  2. Yes, systemd.
  3. Yes, when the computer is turned on, the udev rule recognises the changes between the AC states.

I also tried to change the numbering of the udev rule from 50 to 00 but this did not help either.

The files I am using:

[orschiro@thinkpad ~]$ cat .scripts/powersaving

!/bin/bash

case $1 in battery) echo "Enable screen power saving" echo 5 > /sys/class/backlight/acpi_video0/device/backlight/acpi_video0/brightness ;; AC) echo "Disable screen power saving" echo 14 > /sys/class/backlight/acpi_video0/device/backlight/acpi_video0/brightness ;; esac

[orschiro@thinkpad ~]$ cat /etc/udev/rules.d/00-powersaving.rules SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/home/orschiro/.scripts/powersaving battery" SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/home/orschiro/.scripts/powersaving AC"

Do I need something else to have the udev rule running and checking the AC state on boot?

Hjdskes commented 11 years ago

I think I got it. Udev checks the state of everything pretty early on my laptop, so perhaps it does that even before /home gets mounted. Seeing that your script is in /home, that might explain why it's not being run yet.

So, could you please move the script to your root partition, for example /usr/local/bin?

orschiro commented 11 years ago

Thank you. Now I tried different things without success. As such changed the numbering of XX-powersaving.rules, moved the powersaving script to /usr/bin but still nothing on boot. I am running out of ideas. Are you using the exact same udev rule and script which are in your repo or maybe a later version?

Or do I need the systemd hook?

Robert

On 17 December 2012 12:32, Unia notifications@github.com wrote:

I think I got it. Udev checks the state of everything pretty early on my laptop, so perhaps it does that even before /home gets mounted. Seeing that your script is in /home, that might explain why it's not being run yet.

So, could you please move the script to your root partition, for example /usr/local/bin?

— Reply to this email directly or view it on GitHubhttps://github.com/Unia/powersave/issues/1#issuecomment-11438432.

Hjdskes commented 11 years ago

Sorry for the late reply, I never noticed you had updated your post. Please make a new post instead of editing it, I do get notifications on new posts.

Yes, I'm using everything as provided here. Is there anything in journalctl?

orschiro commented 11 years ago

No, there is nothing in journalctl. I already tried all different sorts of rules:

[orschiro@thinkpad ~]$ ls /etc/udev/rules.d/
00-powersaving2.rules     01-powersaving.rules      50-powersaving.rules      60-powersaving2.rules     90-powersaving.rules      powerdown3.rules
00-powersaving.rules      0-powersaving2.rules      51-powerdown.rules        60-powersaving.rules      network_persistent.rules  powerdown.rules
01-powersaving2.rules     50-powerdown.rules        51-powersaving.rules      90-powersaving2.rules     powerdown2.rules          
[orschiro@thinkpad ~]$ cat /etc/udev/rules.d/*
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}!="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}!="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
KERNEL=="AC", SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/powersaving battery"
KERNEL=="AC", SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_AC_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_AC_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}!="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/powersaving AC"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}!="1", RUN+="/usr/local/bin/powersaving AC"
KERNEL=="AC", SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/powersaving battery"
KERNEL=="AC", SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/powersaving AC"
KERNEL=="AC1", SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/powersaving battery"
KERNEL=="AC1", SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/powersaving AC"
KERNEL=="AC0", SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/powersaving battery"
KERNEL=="AC0", SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/powersaving AC"

I cannot think of a problem with the path to the power_supply system since it works fine during runtime.

[orschiro@thinkpad ~]$ cat /sys/class/power_supply/AC/online
1

As a workaround I now check the AC state on boot in my .xinitrc. Not the best way but at least one that is working...

Hjdskes commented 11 years ago

The rules themselves should be fine, seeing they work properly once the system is booted.

Are you running a custom kernel? I'm quite busy at the moment (gotta finish my work before 16.30, then I gotta go for a running event in another town) but you can try Googling yourself on udev not starting/reading/applying rules at boot.

orschiro commented 11 years ago

Hi Unia,

No I am not running a custom kernel. But that does not matter. I will simply stick with my autostart script:

# Run powersaving script
AC=$( cat /sys/class/power_supply/AC/online )

if [ $AC = "1" ]; then
    /usr/local/bin/powersaving AC
else
    /usr/local/bin/powersaving battery
fi

Nevertheless, thanks for your help!

Robert

orschiro commented 11 years ago

Hi Unia,

I came closer to the issue why my Udev rule is not triggered at boot. In fact it is.

The problem is that the script which is called from the Udev rule needs access to /sys/class/backlight.

At the point in time during boot when the Udev rule is triggered and the script is run, /sys/class/backlight is not available yet.

Do you know any way to delay and let the Udev rule wait a couple of seconds before being triggered at boot?

Hjdskes commented 11 years ago

It would have helped if you had mentioned you had posted on the Arch boards ;)

Sadly, I have no new suggestions other than those already mentioned in your topic. You could look into systemd service files (I'm a noob when it comes to that) and see if there is an option that might work.

orschiro commented 11 years ago

Sorry. I thought you have seen my thread on the forums already a long time ago.

Anyway, if I cannot delay the trigger of the Udev rule on boot, then I will simply delay the powersaving script by a few seconds. Of course, this will not only affect boot but also run time but I can live with three seconds of delay for the brightness to be switched.

[orschiro@thinkpad ~]$ cat /usr/local/bin/powersaving 
#!/bin/sh

case $1 in
    battery)
        echo "Running powersaving on AC in 3 seconds"
        sleep 3
        # screen power saving"
        echo 200000 > /sys/class/backlight/intel_backlight/brightness
    ;;
    AC)
        echo "Running powersaving on battery in 3 seconds"
            sleep 3
        # screen power saving
        echo 4270725 > /sys/class/backlight/intel_backlight/brightness
    ;;
esac