我有一个运行 OMV 的树莓派 4。我创建了一个名为
hasspipe
的命名文件,然后为了使其始终运行,创建了以下脚本:
#!/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
我创建了一个系统服务,它运行得很好。
[Unit]
Description=My Startup Script
After=network.target
[Service]
ExecStart=/path/to/my_script.sh
[Install]
WantedBy=default.target
然后:
sudo systemctl daemon-reload
sudo systemctl enable myscript.service
sudo systemctl start myscript.service
例如,当我运行这样的命令时:
echo "sudo touch /path/check.txt" > /path/to/pipe/hasspipe
文件创建成功。但是当我尝试运行
mpg321
时,它不起作用,我什么也没听到:
echo "mpg321 /path/media/sounds/alert.mp3" > /path/to/pipe/hasspipe
我也尝试过:
echo "mpg321 /path/media/sounds/alert.mp3" > /path/to/pipe/hasspipe
还是什么都没有。但当我跑步时:
mpg321 /path/media/sounds/alert.mp3
它有效。但我不明白的是当我直接运行这个命令时:
while true; do eval "$(cat /path/to/pipe/hasspipe)"; done
然后运行这个:
echo "mpg321 /path/media/sounds/alert.mp3" > /path/to/pipe/hasspipe
它确实有效。所以我很困惑,我不明白我错过了什么。
这是输出:
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?)
我想补充一点,我正在使用的用户名为 pi,只有在运行该用户时一切才正常。我猜系统可能使用root?所以这里可能有东西。
这个脚本适用于我的命名管道。
#!/bin/bash
while true; do eval "$(cat mypipe)"; done
exit 0
当我运行命令时:
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输出:
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