5

I would like to know which signal is sent to the kernel when I unplug my laptop. I need this information to make a program to switch my power mode automatically.

I am currently on Ubuntu 23.10.

Raffa
  • 35,113
  • You can see if the unplug is noticed by the kernel by watching the terminal command sudo journalctl --follow as you unplug the charger. Each message starts with a timestamp, the host that issued the log entry, the process name, the PID in [] of the issuer and a colon (:). Everything after the colon is a message from the programmer, intended to help the user understand the program's behavior. Read man logger. – waltinator Apr 13 '24 at 23:17
  • 2
  • A friendly notice to "Duplicate Close Voters": Some things have changed over the last decade ... Namely the introduction of UPower to Ubuntu and other Linux distros ... Probably it's about time to join the new era :-) I think. – Raffa Apr 14 '24 at 11:20
  • @Raffa I'm not getting your point, the question is basically the same, and the answer to the existing question is still relevant; if what you're saying is that there are new ways to do things and that the linked question's answers should be updated, well, I totally agree, and since both you and Rinzwind have posted good answers here maybe a merge is in order :) – kos Apr 14 '24 at 17:21
  • @kos You are righ but, my point is to keep this recent post open (at least for a while) for new answers with newer methods … People (those with answers below :-) included) tend to overlook very old posts. The linked one for example has one answer with one method that requires installing a package that might not be always available in newer releases … If this post gets closed then no more answers will be possible and most likely the old post will continue to be overlooked and the chance for getting some good recent ways will be closed for the time being. – Raffa Apr 14 '24 at 17:52
  • @Raffa I agree, and I'm not opposed to "let this one live" for a while. The problem is coordination though, at some point it should be closed as a duplicate / merged so to not spread answers to the same question over multiple questions (which doesn't help those looking for an answer), so how do you propose we deal with this? Other than bountying one of the questions / the merged question after the duplicate thing has been dealt with I'm not sure how we should go about slowing down the closing process, I don't think retracting votes and having this "disappear" from the cue would be the solution – kos Apr 14 '24 at 18:16
  • 1
    Well there you go, I retracted mine, hopefully it will survive the cue a little longer. But it shall be closed at some point :) @Raffa – kos Apr 14 '24 at 18:18

2 Answers2

3

which signal is sent to the kernel when I unplug my laptop.

Not exactly "sent to kernel" as you can't intercept that from within user-space AFAIK ... However, related events from the kernel can be listened for ... One way is with udevadm like this:

udevadm monitor -k

I need this information to make a program to switch my power mode automatically.

However, a better, more accurate and straight forward way to do that, IMO, would be to monitor the UPower daemon with the upower tool and filter its output to your liking piped to the tool of your choice e.g. awk like this:

upower --monitor-detail |
awk '/online:.*yes/ && (a != 1) {
    print "AC"
    a = 1
    b = 0
}

/on-battery:.*yes/ && (b != 1) { print "Battery" b = 1 a = 0 }'

... which should print AC when the current power source changes to the power supply or print Battery when the current source of power changes to the battery.

Raffa
  • 35,113
2

Easier method:

Create a loop that constantly checks the status of /sys/class/power_supply/ADP1/online. 0 = wired; 1 = wireless. ADP1 can be something else so using /sys/class/power_supply/*/online is more universal

Rinzwind
  • 310,127
  • 2
    Beware that the exact filename may vary depending on the specific hardware of the laptop. – kos Apr 14 '24 at 01:08
  • I am currently doing something similar but instead of checking the online file, I check the status file – Stanislas Martin Apr 14 '24 at 09:54
  • @Rinzwind Periodic polling isn't the same as waiting for events though. It always forces you to make a trade-off between how much processing time you want to swap, versus how fast you want the response to be. Of course sometimes it's the only solution, but that always indicates incompetent API design. – Graham Apr 14 '24 at 11:05
  • @kos Yes, indeed it might change but, should mostly start with ADP* IIRC so something like cat /sys/class/power_supply/ADP*/online should work for any ... Also the online file seems to only be present under the power adapter directory and I don't recall ever seeing it under the battery directory ... so, even, cat /sys/class/power_supply/*/online should equally work at least on recent Ubuntu versions (given that is run in a shell for globbing to happen) I think. – Raffa Apr 14 '24 at 11:54
  • yes there should only be 1 /online so /*/online would cover all. old version used BAT0 iirc. – Rinzwind Apr 14 '24 at 12:05
  • 1
    @Raffa It will either be AC* or ADP* depending on the firmware, and there may actually be two entries if the system supports both a more traditional AC adapter and USB-PD charging (though most systems like that that I’ve seen have only one entry that covers both). The possibility of multiple entries should be checked before just globbing the whole directory tree (since if there are you will instead get 00, 01, 10, or 11 out of that cat command), but if there is only one that should work. – Austin Hemmelgarn Apr 14 '24 at 12:30
  • ah @AustinHemmelgarn not really an issue coding wise: just check for the presense of a 1 ;-) – Rinzwind Apr 14 '24 at 12:35
  • @AustinHemmelgarn Oh yeah and there is also the possibility of the presence of two or more power adapters (even some laptops have two power supplies nowadays) but as Rinzwind says "checking for a 1" might be a workaround ... However, if I'm not mistaken the kernel relies on "ACPI" calls to build those sysfs directories and while ACPI methods exist on x86 based platforms, there is no guarantee this will be the case on all other platforms AFAIK. – Raffa Apr 14 '24 at 12:52
  • @Raffa ACPI gets used if the system is an ACPI platform (for example, essentially all x86 platforms and any non-x86 UEFI platform), but other mechanisms can be used as well (for example, the device tree on OpenFirmware platforms), though the naming is likely to be different. – Austin Hemmelgarn Apr 14 '24 at 13:04