Python Forum

Full Version: How do I make RPi run script on startup?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a RPi2 running a script indefinitely with a scheduler.  But when the power goes out, which is often, the RPi2 reboots and I have to type "python scriptname.py" again to get it back up and running.

How do I make my RPi2 always run that script again after every reboot?
You can add it to your bashrc file at the bottom to start at your login

edit your script and add the shebang line
Quote:
#!/usr/bin/env python


then make it executable
chmod a+x scriptname.py
then verify it runs as
./scriptname.py
then put that at the bottom of the file .bashrc
#Mars personal mods
Ok I need to create a tmux session and then run 2 scripts. Would this work?

#FIRST:run create tmux session
#SECOND: run reconnect.py to reconnect serialbt on rpi to bt relay board
#THIRD: run tsrb430.py to restart scheduler indefinitely

echo Running at boot...
sudo tmux new -s tsrb430
python /Documents/python/reconnectbt2tsrb.py
python /Documents/python/tsrb430.py
I'm sorry, but would that code be OK?

I'm afraid of adding it and then getting my RPi stuck in some infinite loop or something.

I tried doing:
chmod a+x scriptname.py but I got this:

chmod a+x reconnect.py
chmod: changing permissions of ‘reconnect.py’: Operation not permitted
In order to run chmod you have to be root.

sudo chmod +x reconnect.py
Thx! That did it!

Do you know if I can include that tmux new -s sessionName in the .bashrc? It wont freeze up my rpi? :-)
You can. Start the tmux session, run what you want. Until you connect to the session the terminal will be free to use it.