[Solved]Need help to create a Pipewire loopback script

Hi there, new Bazzite user here, with Gnome, on a desktop.
I’m bit new to Linux and trying to familiarize myself with bash scripting and terminal usage.

Here is my situation i connected the headphone port of my nintendo switch to the rear line-in of my PC and set a loopback with pipewire, so that the sound of my console passthrough my pc to my audio setup.

So far I managed to make a script to start the loopback using pw-loopback.
It works as expected, but if i want to stop it, the only way i have found is to launch pavu and click on terminate playback from there.
Is there a command i could use to stop it from a script ?
Better yet is there a way to make a script check if pw-loopback is already running then terminate it, if not launch it. That way i could toggle the sound on/off from a single script.

Thank you for your help.

Maybe?

#!/bin/bash

PW_LOOPBACK_PID_FILE="/var/run/pw-loopback.pid"

if [ -f "$PW_LOOPBACK_PID_FILE" ]; then
  PW_LOOPBACK_PID=$(cat "$PW_LOOPBACK_PID_FILE")
  if kill -0 "$PW_LOOPBACK_PID" > /dev/null 2>&1; then
    # pw-loopback is running, kill it
    kill "$PW_LOOPBACK_PID"
  else
    # pw-loopback is not running, remove stale pid file
    rm "$PW_LOOPBACK_PID_FILE"
  fi
else
  # pw-loopback is not running, start it
  pw-loopback &
  PW_LOOPBACK_PID=$!
  echo "$PW_LOOPBACK_PID" > "$PW_LOOPBACK_PID_FILE"
fi

Thank you for your help but it give me a permission error.

line 18: /var/run/pw-loopback.pid: Permission denied

Ok I tried this one and it’s seems to be working.

#!/bin/bash

PW_LOOPBACK_PID=$(pgrep pw-loopback)

if [ -n "$PW_LOOPBACK_PID" ]; then
  kill -9 $PW_LOOPBACK_PID
 else
  /usr/bin/pw-loopback &  # Start pw-loopback in the background
 fi

Thank you ! Your script does allow me to kill the loopback but it would not start one if non existed.

I change your script to

#!/bin/bash

PW_LOOPBACK_PID=$(pgrep pw-loopback)

if [ -n "$PW_LOOPBACK_PID" ]; then
  kill -9 $PW_LOOPBACK_PID
 else
  pw-loopback # Start pw-loopback in the background,
 fi

Then i create a desktop entry :

[Desktop Entry]
Name=Switch Sound On/Off

Exec='/home/Kinos/switch_sound_on-off.sh'
Terminal=false
Type=Application

And it works perfectly, i can toggle the sound coming from console on and off and with no terminal window opening ! Thank you !!

1 Like

Strange /usr/bin/pw-loopback didn’t work for you.

What is output of whereis pw-loopback

pw-loopback: /usr/bin/pw-loopback /usr/share/man/man1/pw-loopback.1.gz

Same as mine :grin:

Anyway glad it all worked out!

1 Like