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.
1 Answer 1
$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.
omxplayer /full/path/to/video> /dev/null 2>/dev/null &