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.
linux bash php cron audio
Jeff grunewald
source share