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.
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.
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.
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
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
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
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
sudo journalctl --followas 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. Readman logger. – waltinator Apr 13 '24 at 23:17