title: Audio through Cron Job
body: <p>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. </p>
  
  <p>Extra details:</p>
  
  <p>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) </p>
  
  <p>Here is my test code for just the audio</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>This includes the definition of my audio playback function and the actual call. </p>
  
  <p>Let me know if I can provide any more information to clarify this. </p>
  
  <p>EDIT 1:</p>
  
  <p>I have taken into account some of the suggestions and have modified my code a little bit. </p>
  
  <p>My crontab now looks like the following</p>
  
  <pre><code>* * * * * /usr/bin/python /home/pi/AlarmClock/helper.py &gt; /home/pi/AlarmClock/output.out
  </code></pre>
  
  <p>This just sends the output of the python script to the file output.out</p>
  
  <p>The helper.py file was also updated</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>EDIT SOLVED!:
  This other question completely solved the issue. 
  <a href="https://stackoverflow.com/questions/42497130/audio-doesnt-play-with-crontab-on-raspberry-pi">Audio doesn&#39;t play with crontab on Raspberry Pi</a></p>