Are you looking for a way to automatically launch your favorite music player when you connect a Bluetooth device (E.g. Headset or Speaker) to your Linux system? This guide will show you how to set up an automation to automatically open a Music app when connecting a Bluetooth device using systemd and a simple Bash script. We’ll use Rhythmbox as the music player, but you can replace it with any other app.
I tested this setup with my Fingers Bluetooth headset on Debian 12 Cinnamon desktop system, and it worked perfectly!
Table of Contents
Steps to Automatically Open a Music App on Bluetooth Connection in Linux
Step 1: Find Your Bluetooth Device MAC Address
Before setting up the script, you need to find the MAC address of your Bluetooth device. A MAC address is a unique identifier for your device.
A. Turn on Your Bluetooth Device
Ensure your Bluetooth device is turned on and in pairing mode.
B. List Connected Bluetooth Devices
Open a terminal and run:
bluetoothctl devices
This will show a list of Bluetooth devices along with their MAC addresses. Identify the correct MAC address for your device.
Sample Output:
Device 01:B6:ED:14:1F:8F FINGERS FC-SoundStorm
Note down the MAC address (e.g., 01:B6:ED:14:1F:8F
). You’ll need this in the next steps.
Step 2: Create a Script to Detect Bluetooth Connection
First, create a script that checks if your Bluetooth device is connected and launches Rhythmbox if it isn’t already running.
Open a terminal and create a new file:
nano ~/bluetooth-music.sh
Add the following script:
#!/usr/bin/env bash
# ------------------------------------------------------------------
# Script Name: bluetooth-music.sh
# Description: A Bash script to Auto-launch Music Player
on Bluetooth Connect
# Website: https://gist.github.com/ostechnix
# Version: 1.0
# ------------------------------------------------------------------
DEVICE_MAC="01:B6:ED:14:1F:8F"
APP="rhythmbox"
FLAG_FILE="/tmp/bluetooth_music.flag"
# Check if Bluetooth device is connected
bluetoothctl info "$DEVICE_MAC" | grep -q "Connected: yes"
if [ $? -eq 0 ]; then
# If Rhythmbox is not running and wasn't manually closed, start it
if ! pgrep -f "$APP" > /dev/null && [ ! -f "$FLAG_FILE" ]; then
DISPLAY=:0 "$APP" &
fi
else
# Remove flag when Bluetooth disconnects
rm -f "$FLAG_FILE"
fi
- Replace
01:B6:ED:14:1F:8F
with your Bluetooth device’s MAC address. - Replace
rhythmbox
with the command for your preferred music app (e.g.,clementine
,spotify
,vlc
).
Save and exit the file by pressing Ctrl + X
, then Y
, then Enter
.
Run the following command to make the script executable:
chmod +x ~/bluetooth-music.sh
Step 3: Create a systemd Service to Run the Script
Now, we need to set up a systemd service to run the script in the background.
Create the systemd service file:
nano ~/.config/systemd/user/bluetooth-music.service
Add the following configuration:
[Unit] Description=Auto-launch Music Player on Bluetooth Connect After=bluetooth.target [Service] ExecStart=/bin/bash -c 'while sleep 2; do ~/bluetooth-music.sh; done' Restart=always Environment=DISPLAY=:0 Environment=XDG_RUNTIME_DIR=/run/user/%U [Install] WantedBy=default.target
Make sure the the path to the bluetooth-music.sh
file is correct in the service configuration. Press CTRL+O
and CTRL+X
to save and close the file.
Run the following commands to enable and start the service:
systemctl --user daemon-reload systemctl --user enable bluetooth-music.service systemctl --user start bluetooth-music.service
Now, when your Bluetooth device connects, Rhythmbox will open automatically!
A small issue with this approach is if you close Rhythmbox application, the script will restart it automatically. This happens because the script checks every few seconds if your Bluetooth device is connected. If Rhythmbox
isn't running, the script launches it again. You may not like this and feel it is annoying.
To prevent this behaviour, do the following step.
Step 4: Prevent Rhythmbox from Restarting After Manual Closure
Since the script keeps reopening Rhythmbox
, use a flag file to indicate when you close it manually.
To do so, create an alias to manually close Rhythmbox:
echo "alias closemusic='touch /tmp/bluetooth_music.flag && pkill rhythmbox'" >> ~/.bash_aliases
This creates a command called closemusic
that closes Rhythmbox and sets a flag to prevent it from reopening.
Please note that we already have defined the /tmp/bluetooth_music.flag
file in our script that we created earlier in Step 2.
Now, the script checks for the file /tmp/bluetooth_music.flag
. If it exists, Rhythmbox won’t restart.
Reload ~/.bash_aliases
file:
source ~/.bash_aliases
From now on, you can run the closemusic
command instead of closing Rhythmbox
normally:
closemusic
This tells the script not to reopen Rhythmbox unless your Bluetooth device disconnects and reconnects.
Step 5: Troubleshooting
If Rhythmbox does not launch or the service behaves unexpectedly, try these steps:
A. Check if the script runs manually
Run the script to see if it works:
./bluetooth-music.sh
If it doesn’t start Rhythmbox, check if your Bluetooth device is connected:
bluetoothctl info 01:B6:ED:14:1F:8F
Make sure Connected: yes
appears in the output.
Sample Output:
Device 01:B6:ED:14:1F:8F (public)
Name: FINGERS FC-SoundStorm
Alias: FINGERS FC-SoundStorm
Class: 0x00260404
Icon: audio-headset
Paired: yes
Bonded: yes
Trusted: yes
Blocked: no
Connected: yes
LegacyPairing: no
UUID: Headset
[...]
B. Verify the systemd service status
systemctl --user status bluetooth-music.service
Look for errors in the output.
C. Check the logs for detailed errors
journalctl --user -u bluetooth-music.service -n 50 --no-pager
If you see Cannot open display
, confirm that DISPLAY=:0
is set in the service file.
D. Check the path of the Script in Systemd configuration
Make sure you have defined the correct path of the script bluetooth-music.sh
in your systemd configuration.
E. Restart the service after changes
systemctl --user restart bluetooth-music.service
Conclusion
With this setup, your music player will automatically launch when you connect your Bluetooth device, and you have full control to stop it when needed. This method ensures a smooth experience on Linux without unnecessary restarts.
If you found this guide helpful, please share it with fellow Linux users!
Featured Image by LIN LONG from Pixabay.