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.

7
  • 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 at 23:17
  • 2
    Does this answer your question? How to check if the AC laptop adapter is plugged or not?
    – kos
    Apr 14 at 1:16
  • 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 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 at 17:21
  • 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 at 18:18

2 Answers 2

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.

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

9
  • 2
    Beware that the exact filename may vary depending on the specific hardware of the laptop.
    – kos
    Apr 14 at 1:08
  • I am currently doing something similar but instead of checking the online file, I check the status file Apr 14 at 9: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 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 at 11:54
  • 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. Apr 14 at 12:30

You must log in to answer this question.

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