I have a bluetooth headset and I need to execute a .sh file everytime I connect it to make it work. I don't want to troubleshoot this bug since I know there's already people looking into it.

But I'm interested on a way to run that .sh script every time I connect a bluetooth device. And, if it's possible, every time I connect a headset/this specific headset.

2 Answers 2


Look into udev rules. See the ArchWiki article on udev rules and this question about running a script when a USB device is plugged in.

There are also already many questions on this site regarding bluetooth and udev rules. See:

  • The +Run key in a Udev-Rule is not longer working under systemd. ps -p 1 -o comm=
    – user192526
    Nov 11, 2016 at 19:09

Well, I would try this:

  1. Open a shell (bash, sh, ksh) and type: set -e (enables errorhandling)
  2. Insert Your BT- device
  3. Type: echo $?

You should get a number larger than 0, which is an errorcode. Depending on which shell you use, there might be set some environment variables:

  • ERR=$?=Errorcode
  • ERL=Linenumber if trap was detected in a script.
  • $?=0 then there is no errorcode, and U dont need to read further.

Write a script, or test by hand:

trap 'ERR=$?;errorhandler' listofcodes

errorhandler is the script that decides what todo when any of the listofcodes occur.

The list could be 1 10 14 15 (17 18 ...) or just 1 number, the one you want to catch.

# script errorhandler
case $ERR in
#    here U can take action or perhaps just ignore
#    action if errorcode was different from 1



You would place line 1-4 in /etc/profile or similar to make it systemwide.

In /etc there should be an rc.d or init.d containg start and stop scripts related to boot and change in runstate. There should be a scriptfile for your BT device, check it out, but don't change content unless ...

If the errorcode in line 2 is zero then you must dig deeper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.