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.

JVarhol
6781 gold badge5 silver badges17 bronze badges
asked Feb 1, 2014 at 14:41
2
  • 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 & Commented Feb 1, 2014 at 19:33
  • @kyle2k Did foibs comment answer your question? Commented Feb 3, 2014 at 0:11

1 Answer 1

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.

answered May 10, 2015 at 0:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.