I want to execute a script on headphones disconnect but I resent the idea of constant polling of the status when there is already some code executed when it's changed.
I want to execute a script on headphones disconnect but I resent the idea of constant polling of the status when there is already some code executed when it's changed.
In most systems if not all, ACPI can handle this event. To test that:
acpi_listen
Unplug & replug headphones, example output: (mic/ears share in same jack on my laptop)
Put
your-script.sh
in/etc/acpi/
Add an event trigger file for your script in
/etc/acpi/events/
Check the other files there to learn from.
You may need to restart
acpid
service to reload changed rules in/etc/acpi/events/
Reference:
man acpid
The current version of your script now contains a
sleep 0.25
command.sleep
is timer-based so doesn't use any processing cycles while sleeping…It does use a very tiny bit of CPU to set up the timer, but
sleep 1
(sleep 1 second),sleep 60
(sleep for a minute) andsleep 86400
(sleep for a day) all use the same number CPU cycles.Using ACPI however is the perfect solution as ACPI is event-driven instead of polling-driven.