问题
Does anyone know why audio wouldn't run through a cron job even though it runs perfectly fine when run through the command line. I have a python script that plays audio through a bluetooth speaker and when I run it on the command line (python helper.py) it plays fine, but running it through cron doesn't seem to work.
Extra details:
I am doing this on a Raspberry Pi that I have connected to a bluetooth speaker. I have a display connected to the raspberry pi (not doing it headless but that is the end goal)
Here is my test code for just the audio
import pygame
from pygame import mixer
def playFile(filePath):
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
print('I am here')
while pygame.mixer.music.get_busy():
continue
playFile('/home/pi/AlarmClock/alarm2.ogg')
This includes the definition of my audio playback function and the actual call.
Let me know if I can provide any more information to clarify this.
EDIT 1:
I have taken into account some of the suggestions and have modified my code a little bit.
My crontab now looks like the following
* * * * * /usr/bin/python /home/pi/AlarmClock/helper.py > /home/pi/AlarmClock/output.out
This just sends the output of the python script to the file output.out
The helper.py file was also updated
import sys
import pygame
from pygame import mixer
#fp = open('/home/pi/AlarmClock/erurfile.txt', 'a')
#sys.stdout = sys.stderr = fp
print('at the top')
# Playing media files
def playFile(fileName):
pygame.mixer.init()
pygame.mixer.music.load(fileName)
pygame.mixer.music.play()
print('in the method')
while pygame.mixer.music.get_busy():
continue
print('about to run method')
playFile('/home/pi/AlarmClock/alarm2.ogg')
print('finished running method')
EDIT SOLVED!: This other question completely solved the issue. Audio doesn't play with crontab on Raspberry Pi
回答1:
The problem could lie in many places so the steps below should help identify where ti is broken.
I would start with creating a shell script, running that from cron and checking that it works. So create a text file called my_cron_job.sh
that has the following lines:
#!/bin/sh
date >> /tmp/cron.log 2>> /tmp/cron.err
Amend you cron job to run my_cron_job.sh
and every minute you should have a new line in the /tmp/cron.log
file. The /tmp/cron.err
should be empty.
The next question is "can we run a Python script from cron?". I am not a Python person so I will assume that you have a "HelloWorld.py" that has something like:
print('Hello World')
We now need to run this from our cron job so edit my_cron_job.sh
to read:
#!/bin/sh
python HelloWorld.py >> /tmp/cron.log 2>> /tmp/cron.err
When you check /tmp/cron.log
it should now have "Hello World" appearing every minute. If this is not the case then you need to work out why and it is probably environmental. If you need to add to the environment e.g. modify the PATH then do this before the python
command.
You may have something like:
#!/bin/sh
PATH=$PATH:/usr/local/bin
python HelloWorld.py >> /tmp/cron.log 2>> /tmp/cron.err
If you can get HelloWorld.py
to run then you should be able to get your music player to run.
Some other notes:
- Cron will usually email the output of a command to you which should help. You may need to install a simple text mail client on the Pi to get that (I am very old school so like mailx).
- As stated I don't know Python, but the end of your program looks like it is in a tight loop waiting for
get_busy()
to be false. At a minimum I would insert some form of sleep in here, or look for a better way.
Good luck.
来源:https://stackoverflow.com/questions/43482003/audio-through-cron-job