Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么赢了';我的PHP cron脚本不能播放声音吗?_Php_Linux_Bash_Audio_Cron - Fatal编程技术网

为什么赢了';我的PHP cron脚本不能播放声音吗?

为什么赢了';我的PHP cron脚本不能播放声音吗?,php,linux,bash,audio,cron,Php,Linux,Bash,Audio,Cron,我正在开发一个小应用程序来按时间表敲响学校的钟声,这个程序可以从网站上更新。一切都很好,除了被安排为cron作业的脚本在脚本运行时不会播放声音之外。我在脚本中添加了输出管道和echo命令,以验证cron是否正在运行它,但播放声音的部分不起作用。从CLI手动运行脚本时,脚本可以正常工作 脚本为日程安排中的每个时段提取一个时间和一个声音文件,然后将与声音文件关联的时间与当前时间进行比较——如果它是匹配的,它将 exec("/usr/bin/aplay /var/www/site/".$soundfi

我正在开发一个小应用程序来按时间表敲响学校的钟声,这个程序可以从网站上更新。一切都很好,除了被安排为cron作业的脚本在脚本运行时不会播放声音之外。我在脚本中添加了输出管道和echo命令,以验证cron是否正在运行它,但播放声音的部分不起作用。从CLI手动运行脚本时,脚本可以正常工作

脚本为日程安排中的每个时段提取一个时间和一个声音文件,然后将与声音文件关联的时间与当前时间进行比较——如果它是匹配的,它将

exec("/usr/bin/aplay /var/www/site/".$soundfile);
然后,Cron计划在上学期间每分钟运行此脚本:

* 8-16 * 1-6,9-12 1-5 root /usr/bin/php -f /var/www/site/scripts/playsound.php > /dev/null
同样,如果我在有声音调度时手动运行脚本,声音将通过连接的扬声器播放。当我的测试代码会回显到屏幕或输出到输入的文件时,cron会将输出转储到文件中,确认它正在按计划运行脚本。它只是不能播放剧本中糟糕的声音部分

我已经检查了所有的权限,因为其他所有的功能都正常,所以它们似乎是准确的。我甚至可以编写一个简单的BASH脚本,让Cron按照时间表播放声音,因此系统似乎拥有访问脚本和声音文件的正确组成员身份。我已经为
shell\u exec()
关闭了
exec()
,尝试只使用命令以及命令的绝对路径,并且cron作业计划作为root用户运行。仍然无法理解为什么这一小功能,不幸的是,这是如此关键的程序成功将无法工作


非常感谢您的建议。

使用php interval或javascript(异步-ajax)调用所需的php文件,而不是设置为cron。我想这会解决你的问题

在服务器中执行cron作业时,它不会向浏览器抛出任何输出

Cron作业可以在特定的时间间隔内发送通知/邮件、更新/插入数据库


对于您的需求,您必须通过浏览器运行脚本。

正如其他人在评论中提到的,您确实必须保存日志,然后检查它们以寻找线索

根据我的经验,使用由PHP执行的专用脚本是有益的。您可以将声音文件的名称作为参数提供给bash脚本。例如:

exec("/path/to/script.sh $SOUNDFILEPATH");
在脚本中,您可以设置工作目录、环境变量等

当然,如果你想避免学生得到恶作剧的想法,你必须避开变量

当事情变得艰难时,我总是求助于一种技术,那就是让一个脚本在cron下运行,该脚本由存储在say/tmp/中的文件控制,您可以通过web服务器和PHP写入该文件

因此,只需让PHP将控制数据写入/tmp(或任何其他可写目录)中的文件,并让您的测试用户bash脚本通过cron定期监视它。它可以简单到只有一行指向您的案例中的声音文件


这对我来说总是很有效,没有什么意外。

可能是
/dev/snd/pcmC0D0p
的权限问题。(这是用于ALSA卡0:device 0:playback的设备。)如果服务器上正在运行桌面会话,则可能有一个pulseaudio守护进程保持设备打开

为多个用户同时播放声音进行设置是一件麻烦事,需要在非零拷贝的低性能模式下配置pulseaudio,所以不要这样做。只要确保服务器可以在需要时以独占方式打开声音设备

要查看ALSA声音设备是否打开(无论暂停状态或w/e):

因此,我的第一张声卡上的第一个播放PCM是打开的,但第二个PCM(主板音频的S/PDIF输出)是关闭的。声音设备的进一步打开仅起作用,因为默认的ALSA设置使“默认”设备成为pulseaudio包装器
hw:0
将失败,因为它很忙,并且此声卡没有硬件混音器


(有趣的事实:一些旧声卡,例如一些PCI Soundblaster卡,支持硬件设备的多个打开。多个PCM数据流可以被DMAed到该卡,在那里它们会被其DSP混合。除非我完全错了,内核驱动程序混合><,但无论如何,如果你有pulseaudio,你不需要它。)据我所知,
cron | crontab
在它自己的环境中运行;e、 g.参见以下内容中的公认答案:

因此,即使
aplay
在作为普通用户或root用户调用时(
su
|
sudo
)会播放声音(
WAV
)文件,但当从
cron
调用该文件时,它不会播放该文件——例如,执行包含
aplay
语句的bash(.sh)脚本

这在我的Arch Linux系统上是有效的

这是我的
cron\u通知。sh
脚本:

#!/usr/bin/bash

# /mnt/Vancouver/Programming/scripts/cron_notification.sh

# For use with crontab [ sudo gedit /etc/crontab ]
# cron (Arch Linux: cronie) requires full paths:
#   /usr/bin/aplay
#   /usr/bin/notify-send

for i in 1 2 3 4 5
  do
    #aplay alarm.mp3    ## << aplay cannot play MP3 files; use WAV
    # ----------------------------------------
    # NEEDED TO RUN 'aplay' FROM crontab:
    # https://unix.stackexchange.com/questions/231941/cant-run-aplay-as-root
    # https://www.reddit.com/r/linuxquestions/comments/37vcbo/playing_audio_from_a_cronjob/
    # PulseAudio needs XDG_RUNTIME_DIR, so:

    XDG_RUNTIME_DIR=/run/user/`id -u` /usr/bin/aplay /mnt/Vancouver/Programming/scripts/PHASER.WAV
    # ----------------------------------------
    sleep 0.25
done

# ----------------------------------------------------------------------------
# "Critical" alerts persist until clicked (i.e., do not appear, then fade after ~20"):
# notify-send -u critical 'Hello Victoria!' 'Countdown has ended!' --icon=dialog-information

/usr/bin/notify-send -u critical 'Hello Victoria!' "It's 3 pm!" -i /mnt/Vancouver/Programming/scripts/alert.jpg
为了便于参考和澄清,我保留了这些评论

您可以从我的网站下载
PHASER.WAV
声音文件,网址:


这似乎更像是针对不同SE站点的问题。对于Ubuntu,这里有一个答案:可能对其他nix版本有所帮助。尝试将此添加到exec:
exec('…>/tmp/cronlog 2>&1')
,如果有任何错误,请检查
/tmp/cronlog
日志文件。如果
aplay
需要X,则需要确保root用户可以访问活动的X会话(请参阅
xhost
命令)。您可能希望使用具有cron作业活动会话的用户。这可能会有帮助:-一个建议:调试cronjob问题时不要抑制错误。提高日志记录级别。有时,通过cron或作业队列运行的东西无法获得通常控制台环境中的环境变量。尝试
PATH=HOME=/usr/bin/aplay/var/w
#!/usr/bin/bash

# /mnt/Vancouver/Programming/scripts/cron_notification.sh

# For use with crontab [ sudo gedit /etc/crontab ]
# cron (Arch Linux: cronie) requires full paths:
#   /usr/bin/aplay
#   /usr/bin/notify-send

for i in 1 2 3 4 5
  do
    #aplay alarm.mp3    ## << aplay cannot play MP3 files; use WAV
    # ----------------------------------------
    # NEEDED TO RUN 'aplay' FROM crontab:
    # https://unix.stackexchange.com/questions/231941/cant-run-aplay-as-root
    # https://www.reddit.com/r/linuxquestions/comments/37vcbo/playing_audio_from_a_cronjob/
    # PulseAudio needs XDG_RUNTIME_DIR, so:

    XDG_RUNTIME_DIR=/run/user/`id -u` /usr/bin/aplay /mnt/Vancouver/Programming/scripts/PHASER.WAV
    # ----------------------------------------
    sleep 0.25
done

# ----------------------------------------------------------------------------
# "Critical" alerts persist until clicked (i.e., do not appear, then fade after ~20"):
# notify-send -u critical 'Hello Victoria!' 'Countdown has ended!' --icon=dialog-information

/usr/bin/notify-send -u critical 'Hello Victoria!' "It's 3 pm!" -i /mnt/Vancouver/Programming/scripts/alert.jpg
# /etc/crontab

# system-wide crontab
# edit: sudo {your favorite text editor: gedit; geany; ...} /etc/crontab
# https://crontab.guru      ## online crontab values checker, planner

# =====================================================================
# SHELL
# =====================================================================

# cron (crontab) requires full paths:
SHELL=/usr/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# =====================================================================
# ARCH LINUX-RELATED [Arch Linux x86_64]
# =====================================================================

# cron | https://wiki.archlinux.org/index.php/Cron
# Will install, use "cronie" cron (crontab):
# Install cronie:  sudo pacman -Syu cronie
#    Enable cron:  sudo systemctl enable --now cronie.service
#     Start cron:  sudo systemctl start cronie.service
#   Restart cron:  sudo systemctl restart cronie.service

# =====================================================================
# APLAY NOTIFICATION (3:00pm M-F)
# =====================================================================

# Two issues when running
#   /mnt/Vancouver/Programming/scripts/cron_notification.sh
# from cron:

# ----------------------------------------
# 1. "notify-send":

# https://bbs.archlinux.org/viewtopic.php?id=216912
# notify-send also needs access to your DBUS_SESSION_BUS_ADDRESS. Assuming that your UID is 1000:

DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"

# More here: https://wiki.archlinux.org/index.php/Desktop_notifications#Usage_in_programming

# ----------------------------------------
# 2. "aplay":

# TO RUN 'aplay' FROM crontab:
# https://www.reddit.com/r/linuxquestions/comments/37vcbo/playing_audio_from_a_cronjob/
# PulseAudio needs XDG_RUNTIME_DIR, so:

# XDG_RUNTIME_DIR=/run/user/`id -u` /usr/bin/aplay /mnt/Vancouver/Programming/scripts/PHASER.WAV

# Line above added to "/mnt/Vancouver/Programming/scripts/cron_notification.sh" script.

# ----------------------------------------

# m    h    dom    mon    dow    user    nice    command

# "At 15:00 on every day-of-week from Monday through Friday”
# [https://crontab.guru/#0_15_*_*_1-5]:
0    15    *    *    1-5    victoria    nice -n 19    /usr/bin/bash /mnt/Vancouver/Programming/scripts/cron_notification.sh

# NOTE -- running as user ("victoria"), not "root".

# Test - every minute:
#*    *    *    *    1-5    victoria    nice -n 19    /usr/bin/bash /mnt/Vancouver/Programming/scripts/cron_notification.sh