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