I am on UBuntu 16.04. I have asked a question here regarding headphones plug unplug event. What I tried didn't work. I want to use acpi_listen command to listen to headphones connected event and display a message using notify-send. How to use acpi_listen in a shell script?
Ubuntu – Using acpi_listen command in a shell script
acpibashheadphonesscriptssound
Best Answer
Writing script like that is fairly simple - you need to pipe
acpi_listen
towhile IFS= read -r line ; do ... done
structure, and take care of handling the events within that structure. Theread
shell builtin command will wait for a line of text fromacpi_listen
and processing will occur when theif
statement sees that the line contains appropriate text. Alternatively, one could usecase
statement for better portability of the script.Here's the simple script I personally would use. Tested on Ubuntu 16.04 LTS
Note that if you plan to run this from cron job or via
/etc/rc.local
, you would need to export yourDBUS_SESSION_BUS_ADDRESS
fornotify-send
to work.