Running mpg321 through a named pipe doesn't work

71 Views Asked by At

I have a raspberry pi 4 running OMV. I created a named file called hasspipe and then in order to keep it running at all times a created the following script :

#!/bin/bash
while true; do
    eval "$(cat /path/to/pipe/hasspipe)" >> /path/hass_output.txt 2>&1
    sleep 1  # Adding a small delay between iterations
done

And I created a system service and it is running just fine.

[Unit]
Description=My Startup Script
After=network.target

[Service]
ExecStart=/path/to/my_script.sh

[Install]
WantedBy=default.target

Then :

sudo systemctl daemon-reload
sudo systemctl enable myscript.service
sudo systemctl start myscript.service

When I run a command like this one for example :

echo "sudo touch /path/check.txt" > /path/to/pipe/hasspipe

The file is created successfully. But when I try to run mpg321 it doesn't work and I don't hear anything :

echo "mpg321 /path/media/sounds/alert.mp3" > /path/to/pipe/hasspipe

I tried also :

echo "/usr/bin/mpg321 /path/media/sounds/alert.mp3" > /path/to/pipe/hasspipe

Still nothing. But when I run :

mpg321 /path/media/sounds/alert.mp3

It works. But what I don't understand is when I run this command directly :

while true; do eval "$(cat /path/to/pipe/hasspipe)"; done

and then run this :

echo "mpg321 /path/media/sounds/alert.mp3" > /path/to/pipe/hasspipe

It does work. So I'm confused and I don't understand what I'm missing.

Here is the output :

High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
tcgetattr(): Inappropriate ioctl for device

Directory: /path/media/sounds/
Playing MPEG stream from alert.mp3 ...
MPEG 1.0 layer III, 256 kbit/s, 44100 Hz joint-stereo
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.front.0:CARD=0'
ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM front
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Access denied

Can't find a suitable libao driver. (Is device in use?)

I would like to add that the user I'm using is called pi, and it's when running this user that everything works. I'm guessing the system uses root maybe? So there might be something here.

I ran : sudo fuser -v /dev/snd/* And here's what it returned :

pi@raspberrypi:~ $ sudo fuser -v /dev/snd/*
                     USER        PID ACCESS COMMAND
/dev/snd/controlC0:  pulse       877 F.... pulseaudio
                     pi         1168 F.... pulseaudio
/dev/snd/controlC3:  pulse       877 F.... pulseaudio
                     pi         1168 F.... pulseaudio
1

There are 1 best solutions below

1
Dražen Grašovec On

This script works for me with named pipe.

#!/bin/bash

while true; do eval "$(cat mypipe)"; done

exit 0

When i run command:

drazen@HP-ProBook-640G1:~/proba$ echo "mpg321 ~/Music/Rodriguez.mp3" > mypipe 
drazen@HP-ProBook-640G1:~/proba$ High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!

Directory: /home/drazen/Music
Playing MPEG stream from Rodriguez.mp3 ...
MPEG 1.0 layer III, 192 kbit/s, 48000 Hz stereo

PS output:

  40239 ?        Ssl    2:21  \_ /usr/libexec/gnome-terminal-server
  40264 pts/0    Ss     0:02      \_ bash
  51708 pts/0    Sl     1:08          \_ xed
 165720 pts/0    S      0:00          \_ /bin/bash ./script.sh
 165729 pts/0    Sl     0:02          |   \_ mpg321 /home/drazen/Music/Rodriguez.mp3
 165814 pts/0    R+     0:00          \_ ps fax