Why is my PHP cron script not playing sound?

I am working on a small application to ring school bells on a schedule that can be updated from the website. Everything works fine, except for the script, which is scheduled as a cron job, will not play sound when the script starts. I added output commands and echo commands to the script to check if cron is working, but the part that plays the sound is not working. The script works as expected when launched manually from the CLI.

The script extracts the time and sound file for each period of the day in the schedule, then compares the time associated with the sound file with the current time - if it matches, it will be

exec("/usr/bin/aplay /var/www/site/".$soundfile); 

Cron then plans to run this script every minute during the school day:

 * 8-16 * 1-6,9-12 1-5 root /usr/bin/php -f /var/www/site/scripts/playsound.php > /dev/null 

Again, if I manually run the script when there is a scheduled sound, the sound is played through the connected speakers. When I have test code that will echo to the screen or be displayed on the entered file, cron will upload the output to files, confirming that it runs the script on schedule. It just won't play the audio part of the script.

I checked all my rights and since everything else works, they seem accurate. I can even write a simple BASH script to make Cron play the scheduled audio, so it seems that the system has group membership rights to access the script and sound file. I turned off exec() for shell_exec() , tried to use only commands, as well as absolute paths to commands, and the cron job should run as root. I still can’t understand why this small function, unfortunately so important for this program, will not work.

Any advice is appreciated.

+7
linux bash php cron audio
source share
4 answers

Permissions on /dev/snd/pcmC0D0p are /dev/snd/pcmC0D0p problem. (What is the device for the ALSA card 0: Device 0: playback.) If the server has a desktop session, it may have a pulseaudio daemon that keeps the device open.

Setting up multiple users to play sound simultaneously is a mess and requires setting pulseaudio in a low-performance mode that is not a null copy, so don't do this. Just make sure that the server can only open the audio device when necessary.

To find out if an ALSA sound device is open or not (regardless of pause or w / e):

 $ cat /proc/asound/card0/pcm0p/sub0/hw_params access: MMAP_INTERLEAVED format: S16_LE subformat: STD channels: 2 rate: 44100 (44100/1) period_size: 8192 buffer_size: 16384 $ cat /proc/asound/card0/pcm1p/sub0/hw_params closed 

So, the first PCM playback on my first sound card is open, but the second PCM (S / PDIF output for my motherboard) is closed. Opening a sound device further only works because setting ALSA by default makes the device the default shell of pulseaudio. hw:0 will fail because it is busy and there is no hardware mixer on this sound card.

(Satisfactory fact: some old sound cards, such as some PCI Soundblaster cards, supported several hardware device openings. Several PCM data streams may be DMAed onto the card, where they will mix with the DSP. Completely wrong, and the kernel driver mixed>. <But anyway you didn't need pulseaudio if you have one.)

+1
source share

Instead of specifying as cron, use php interval or javascript (asynchronous - ajax) to call the target php file. I think this will solve your problem.

Running cron running on the server will not exit your browser.

Cron Job is good at sending notifications / letters, updating / pasting in db at certain time intervals.

According to your requirements, you should run the script through the browser.

0
source share

As mentioned in the comments, you really should keep logs and then research them for clues.

From what I recall from my experience using a dedicated script that is executed by PHP, it can be useful. You load the bash script the name of the sound file as a parameter. For example:

 exec("/path/to/script.sh $SOUNDFILEPATH"); 

In a script, you can configure the working directory, environment variables, etc.

Of course, you should avoid variables if you want students to not get mischievous ideas.

One of the methods that I always discarded when it gets tough is to have one script run under cron, which is controlled by a file stored in say / tmp /, which you can write through a web server and PHP.

So just run PHP to write control data to a file in / tmp (or any other writable directory) and ask your test user bash script to regularly monitor it through cron. It can be as simple as having one line pointing to the sound file in your case.

It always worked for me without surprises.

0
source share

As I understand it, cron | crontab cron | crontab operates in its own environment; for example, see the accepted answer in:

https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user

Thus, even if aplay will play a sound ( WAV ) file when called as a regular user or as root ( su | sudo ), it will not play this file when it is called from cron - for example, run a bash (.sh) script that contains your aplay ).

This works on my Arch Linux system.

Here is my cron_notification.sh script:

 #!/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 (ie, 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 3 pm!" -i /mnt/Vancouver/Programming/scripts/alert.jpg 

And here is the relevant part of my /etc/crontab file:

 # /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 MF) # ===================================================================== # 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. # ---------------------------------------- # mh 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 

I left comments in place for reference and clarity.

alert.jpg

notification

You can download the sound file PHASER.WAV from my site, here:

http://persagen.com/files/PHASER.WAV

0
source share

All Articles