K2Digital
K2Digital

Reputation: 603

Raspberry Pi Auto Starting a program

So I have a python game (PyGame) running on a raspberry Pi.

I have followed the instructions found on many sites for getting the Raspberry Pi to auto login (those all work), auto run startx, but I'm stuck on getting my program to run once the GUI loads.

Many people (here on StackOverflow and other places) point to this presentation here:

http://www.slideshare.net/SeggySegaran/raspberry-pi-autostarting-a-python-program

I've tried both ways of doing it (putting the desktop file in autostart or putting the command in rc.local

I have opened up a Terminal window and copy / pasted the command to verify there are no typos and the code will run......

sudo python /home/pi/valley.py

and it will run. Is there a way to see a log to find out WHY the program doesn't launch? Is there a better way to get done what I want to get done?

Upvotes: 0

Views: 20208

Answers (4)

user7342556
user7342556

Reputation: 1

You can run your script automatically on startup of raspberry by using crontab. Crontab is table that list all command to perform on scheduled time.

First, you need to edit crontab by using: sudo crontab -e And after this, add following line: @reboot python path-of-your-script & (& should be at the end of line that means command will execute in background).

Save your script and reboot your system. When your system will start, your script will run automatically.

Upvotes: 0

Barry Gackle
Barry Gackle

Reputation: 839

If you are starting X with "startx", you can also just stick your game in your .xinitrc. If your game binary is called "game" and is in your path, just do this:

 echo "game" >> ~/.xinitrc

This works for other commands. Add a "&" if you want the command to keep running in the background.

This is how I start my window manager, load my wallpaper, start a compositor, etc. It is stupid simple, easy to change later, and can do anything you can do at a terminal prompt.

Upvotes: 0

Stacked
Stacked

Reputation: 861

You can achieve this in two ways:

1). Using LXDE autostart.

2). As a service via init.d.

Upvotes: 0

George Profenza
George Profenza

Reputation: 51837

I've got my python script to run at startup doing this:

sudo nano /etc/xdg/lxsession/LXDE/autostart

This will allow you to add an element to run when the LXDE desktop session begins (the raspian default GUI if setup to do from raspi-config)

It will probably have entries like these:

@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash

It's just a matter of adding your script there as well

@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash
@python /home/pi/yourAwesomePyScriptHere.py

If your python script uses GPIO, you need to run that as root (using sudo):

@sudo python /home/pi/yourGPIOScript.py

One thing I do want to point out: always test your script before hand. I mean, run with the absolute path, make sure it still works, try to break it, make sure it's as robust as it can be. If there are errors in your script and you place it at start up you won't see those in a terminal window, but you will hog the cpu with python stuck in a loop at startup.

Also check out this answer on the RPi exchange

Upvotes: 1

Related Questions