Skip to content

Instantly share code, notes, and snippets.

@fatihkaan22
Last active December 16, 2023 05:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fatihkaan22/0cad6b53647c5d887c67b22c260568cb to your computer and use it in GitHub Desktop.
Save fatihkaan22/0cad6b53647c5d887c67b22c260568cb to your computer and use it in GitHub Desktop.
Kills unused spotify pulseaudio instances
#! /bin/bash
spotifyInstances=()
while read LINE1; read LINE2; do
IFS=':-='
read -ra L1 <<< $LINE1
read -ra L2 <<< $LINE2
# trim leading/trailing spaces for error checking
INDEX_STR=$(echo "${L1[0]}" | xargs)
NAME_STR=$(echo "${L2[0]}" | xargs)
if [[ $INDEX_STR != 'index' || $NAME_STR != 'application.name' ]]; then
echo 'ERROR'
exit
fi
INDEX=${L1[1]}
NAME=${L2[1]}
if [[ $NAME == ' "Spotify"' ]]; then
spotifyInstances+=( $INDEX )
fi
done < <(pacmd list-clients | grep -e 'index:' -e 'application.name')
# remove last element (active client)
unset 'spotifyInstances[${#arr[@]}-1]'
for s in ${spotifyInstances[@]}; do
echo "pacmd kill-client $s"
pacmd kill-client $s
done
@vincentbernat
Copy link

vincentbernat commented Jul 18, 2021

If you prefer a one-liner:

pacmd list-clients \
  | awk '($1 == "index:") {i=$2} ($1 == "application.name" && $0 ~ /spotify/) {print i}' \
  | head -n-1 \
  | xargs -n1 pacmd kill-client

@Seanmatthews
Copy link

Seanmatthews commented Jan 26, 2022

Slight modification from above:

pacmd list-clients \
  | awk '($1 == "index:") {i=$2} ($1 == "application.name" && tolower($0) ~ /spotify/) {print i}' \
  | head -n-1 \
  | xargs -n1 pacmd kill-client

@michondr
Copy link

thank you guys! this solved a great headache for me. hopefully this will be solved by spotify at some point, as the thread does not seem to progress

@Dominaezzz
Copy link

Dominaezzz commented Dec 20, 2022

For pipewire people:

pw-dump \
  | jq '.[] | select(.type == "PipeWire:Interface:Client" and .info.props."application.name" == "spotify") | .id' \
  | head -n-1 \
  | xargs --no-run-if-empty -n1 pw-cli destroy

@martinprad0
Copy link

I love you guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment