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
This script works for me with named pipe.
When i run command:
PS output: