1

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

Community
  • 1
  • 1
Jay
  • 157
  • 2
  • 11
  • 1
    What is your cron definition? Its my assumption that you are not invoking the script correctly. – Matt Clark Apr 18 '17 at 20:54
  • * * * * * /usr/bin/python /home/pi/AlarmClock/helper.py – Jay Apr 18 '17 at 21:54
  • 1
    if your script relies on any settings like env vars defined in ~/.bashrc these must be source explicitly ... cronjobs do not invoke a shell like opening up a a terminal would ... also cronjobs are not given a TTY dunno if this might restrict ability to access the audio device – Scott Stensland Apr 19 '17 at 03:20
  • Sorry I'm newer to compsci so I'm not sure what ~/.bashrc is or how to source those in the cronjob explicitly. Also don't know what a TTY is. – Jay Apr 19 '17 at 04:54
  • Does this answer your question? [Audio doesn't play with crontab on Raspberry Pi](https://stackoverflow.com/questions/42497130/audio-doesnt-play-with-crontab-on-raspberry-pi) – 16851556 Nov 14 '22 at 15:41

1 Answers1

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.

7UpMan
  • 63
  • 6
  • Thanks for all the suggestions! I will try all of this and get back to you. Cheers! – Jay Apr 19 '17 at 15:34
  • Here is another update. So I updated my crontab to run the following '* * * * * /usr/bin/python /home/pi/AlarmClock/helper.py > /home/pi/AlarmClock/helper.py' My helper.py code as 4 print statements throughout the code that simply tells me where in the code it is (At the top, In the method, Before method call, after method call) My output file successfully contains the 4 messages meaning that the file was run, but for some reason the output was not sent to the bluetooth speaker I am connected to. – Jay Apr 23 '17 at 01:19