Micael
Posts: 3
Joined: Sun Sep 15, 2013 3:44 pm

Cron job not working correctly

Sat Mar 01, 2014 11:02 am

Hello

This is my first post here and I'm a beginner at Linux and Raspberry Pi.

OS: Raspian installed from NOOBS_v1_3_4 on a Sandisk SDHC card 8 GB.

Here is my problem:
I want to start a game I have created, no video!
It needs to play sound files, use SPI and I2C to show information to the player. when I run it logged in to the RPi it works fine and I can play it several times and there is no problem. Everything works just fine.


I use pygame to play sound files:

Code: Select all

import pygame
pygame.init()
--- some other code-----
pygame.mixer.music.load("/home/pi/sound.mp3")
pygame.mixer.music.play()
The problem starts when I update crontab so it would autostart. As a beginner I found this page: http://www.raspberrypi-spy.co.uk/2013/0 ... sing-cron/ and I followed it. With a test script in Python that communicates with a MCP23017 over I2C it works just fine and and I get this result:

Code: Select all


ps aux | grep /home/pi/test.py
root      2058  0.0  0.3   4584  1452 ?        S    10:27   0:00 sudo python /home/pi/test.py
root      2065  0.1  0.9  10120  4448 ?        S    10:27   0:00 python /home/pi/test.py
pi        2299  0.0  0.1   3544   812 pts/0    S+   10:33   0:00 grep --color=auto /home/pi/test.py
But when I run my game it looks something like this:

Code: Select all

pi      2065  0.1  0.9  10120  4448 ?        S    10:27   0:00 python /home/pi/game.py
The game starts and I hear the beginning of the start sound and then everything (just the Python script, the RPi works fine) stops working.
I have added this to my crontab

Code: Select all

@reboot sudo python /home/pi/game.py >>/home/pi/game-log.log &
The game-log.log file is empty so no help there.

What have I missed?
What can be wrong?

Please let me know if I missed any vital information for helping me solve this problem.

Kind regards
Micael

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Cron job not working correctly

Sun Mar 02, 2014 9:49 pm

I suspect that the underlying problem is that the pygame module turns your code into an x-windows client application, so be be able to run it needs to interact with an x-windows server. My guess is that running your code from cron does not set up the environment variables needed to interact with an x-server.

I don't know how to fix it. Maybe it isn't meaningful to try to run a graphical program in a situation where it cannot access a display?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Micael
Posts: 3
Joined: Sun Sep 15, 2013 3:44 pm

Re: Cron job not working correctly

Mon Mar 03, 2014 8:57 am

Thank You for replying DeeJay!! :)
Didn't know that about X-server

I found this post: http://www.raspberrypi.org/phpBB3/viewt ... 32&t=68224 and it looks promising. I'll try that and see what happens.

Thanks again

Micael

Return to “Troubleshooting”