1

I am trying to have cron run a python script that plays an MP3 file.

It sounded easy but I have been searching and struggling to find the correct solution and was hoping someone could steer me in the right direction.

This code below works fine when I connect via SSH into my headless Raspberry Pi:

#! /usr/bin/env python
# play mp3 from cron task
# Import moduals and functions


import os, sys

os.system('omxplayer -o local Lithium.mp3')



sys.exit()

But it does not work when cron is set to run every 5 minutes.

*/5 * * * * python /home/pi/music/mp3.py &>/dev/null

After searching around I seem to think it should be run as some sort of sub-process? I have tried various bits of code such as popen() and call() to no avail.

Am I heading in the right direction with sub-processes and if so which one I should be looking to use, or is this not possible.

  • I don't know python, but I use this command invoked from a perl script omxplayer /full/path/to/video> /dev/null 2>/dev/null & – foibs Feb 1 '14 at 19:33
  • @kyle2k Did foibs comment answer your question? – Jivings Feb 3 '14 at 0:11
3

$PATH is often not set in cron so use the full path and log err and out to resolve any issues:

*/5 * * * * /usr/bin/python /home/pi/music/mp3.py >/home/pi/music/mp3.log 2>&1

You can use env (env) to check how your cron environment differs from the one configured for ssh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.