Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Start a Python Script from boot

Fri Jun 20, 2014 12:47 pm

Hello everyone, I was hoping someone could help me. I'm working on a project that uses a python script but it needs to open the python script as soon as the Raspberry Pi has booted up. I was wondering if anyone knew of any really good tutorials on this or if someone could describe it themselves.

Thanks, Tekk3y
Last edited by Tekk3y on Fri Jun 20, 2014 4:37 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 31219
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Start a Python Script from boot

Fri Jun 20, 2014 12:55 pm

Add a line in /etc/rc.local (before the exit 0 line) with

/dir/subdir/pythonscriptname parms &

You need to use the whole path to the script.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Fri Jun 20, 2014 3:59 pm

Hello, thanks. I tried this but its saying "can't open file to write'. What should I do?

Thanks, Tekk3y

User avatar
Douglas6
Posts: 4204
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start a Python Script from boot

Fri Jun 20, 2014 4:09 pm

Code: Select all

sudo nano /etc/rc.local

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Fri Jun 20, 2014 4:45 pm

Hello. I tried what you showed above but it doesn't seem to of done anything. I'm fairly new to the Raspberry Pi so i'v attached an image of what i'v written and if you can spot what i'v done wrong then that would be great.

Image

Thanks again, Tekk3y

User avatar
ilguargua
Posts: 73
Joined: Thu Jun 19, 2014 8:14 pm
Location: Livorno - Italy

Re: Start a Python Script from boot

Fri Jun 20, 2014 5:21 pm

Is yor program file beginning with a properly hashbang (eg. #!/usr/bin/python)?
Did make it executable (chmod +x youfilename)?

Cheers,

Ale.

User avatar
Gert van Loo
Posts: 2452
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Start a Python Script from boot

Fri Jun 20, 2014 5:39 pm

You do not have a shell which starts python for you.
So you have to specify the executable and the full paths to everything.
Try this:
/usr/bin/python /home/pi/Desktop/program.py &

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Fri Jun 20, 2014 7:23 pm

Gert van Loo wrote:You do not have a shell which starts python for you.
So you have to specify the executable and the full paths to everything.
Try this:
/usr/bin/python /home/pi/Desktop/program.py &
Hi, I tried this but it still doesn't do anything on boot. :/

Tekk3y

User avatar
Stephanie
Posts: 13
Joined: Tue Jun 17, 2014 12:03 pm
Location: Ontario, Canada
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 12:51 am

What are you expecting it to do? You probably won't see any output on your screen; when the script is run during startup it is not 'attached' to your console.

In other words, depending on what the script does, it might be running, you just can't see it.

Since you've mentioned it was in ~/Desktop I am wondering if you are having your Pi boot directly to the GUI desktop? If so, there's a way to have it run automatically (inside a terminal shell window if necessary) when the desktop boots up.

Using the program path in your earlier post, you would create the following file:

Code: Select all

[Desktop Entry]
Name=My Program
Comment=My Program Auto Start
Exec=/home/pi/Desktop/program.py
Icon=
StartupNotify=true
Terminal=true
X-MultipleArgs=false
Type=Application
Categories=Utility;TerminalEmulator;
From your home directory that would go into .config/autostart/myprogram.desktop
You could open a terminal window and type:

Code: Select all

nano ~/.config/autostart/myprogram.desktop
Then just paste the contents of the above code.

That will make the GUI automatically launch program.py in a text window whenever you start up the Pi.

Hope that is helpful.

Cheers!

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Sat Jun 21, 2014 10:59 am

Hi, when I do this it says "Error Writing /home/pi/.config/autostart/myprogram.desktop: No such file or". Should I have created the autostart file first?

rgrbic
Posts: 128
Joined: Thu Jun 12, 2014 1:07 pm
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 11:40 am

You have to put path to your python script in rc.local file. Open the file with the following command:

Code: Select all

sudo nano /etc/rc.local
and before exit 0 write:

Code: Select all

python /home/pi/Desktop/program.py &
Make sure that your python script starts with the following line:

Code: Select all

#!/usr/bin/env python
At 127.0.0.1
Twitter: @rgrbic
IoT-projects.com

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Sat Jun 21, 2014 12:02 pm

Stephanie wrote:What are you expecting it to do? You probably won't see any output on your screen; when the script is run during startup it is not 'attached' to your console.

In other words, depending on what the script does, it might be running, you just can't see it.

Since you've mentioned it was in ~/Desktop I am wondering if you are having your Pi boot directly to the GUI desktop? If so, there's a way to have it run automatically (inside a terminal shell window if necessary) when the desktop boots up.

Using the program path in your earlier post, you would create the following file:

Code: Select all

[Desktop Entry]
Name=My Program
Comment=My Program Auto Start
Exec=/home/pi/Desktop/program.py
Icon=
StartupNotify=true
Terminal=true
X-MultipleArgs=false
Type=Application
Categories=Utility;TerminalEmulator;
From your home directory that would go into .config/autostart/myprogram.desktop
You could open a terminal window and type:

Code: Select all

nano ~/.config/autostart/myprogram.desktop
Then just paste the contents of the above code.

That will make the GUI automatically launch program.py in a text window whenever you start up the Pi.

Hope that is helpful.

Cheers!
So I tried this and you can see from my previous response what happened the first time but secondly I created the auto start folder as there wasn't one there and added the file but I still don't get the terminal to run the python script once its reached the desktop.

User avatar
Stephanie
Posts: 13
Joined: Tue Jun 17, 2014 12:03 pm
Location: Ontario, Canada
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 1:20 pm

Hi Tekk3y,

.config and autostart are directories that should already be there. If you open a new terminal window and type:
cd .config (return)
then type:
cd autostart (return)
there should not be any errors, and you should see a prompt that looks like this:
~/.config/autostart $

From there you can try entering:
nano myprogram.desktop (return)

And then enter the code in my previous post.

If you get any errors along the way, please share exactly what error you get.

I've done this myself to get a python script to launch upon booting to the desktop, it should work but we might just need to tweak it a bit to be sure.

Oh one other thing you need to verify that your python script has a proper hashbang first line, and that it is executable, as ilguargua said.

I use a slightly different hashbang, but either should work. The first line of my .py file is:

Code: Select all

#!/usr/bin/env python2.7

User avatar
Cancelor
Posts: 751
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start a Python Script from boot

Sat Jun 21, 2014 2:05 pm

I'm trying to follow along on rasbian and I get this....

Code: Select all

pi@raspberrypi ~ $ 
pi@raspberrypi ~ $ cd .config
pi@raspberrypi ~/.config $ ls
enchant  libfm  lxpanel  midori  openbox  pcmanfm  Trolltech.conf
pi@raspberrypi ~/.config $ cd autostart
-bash: cd: autostart: No such file or directory
pi@raspberrypi ~/.config $ ^C
pi@raspberrypi ~/.config $ 
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Sat Jun 21, 2014 2:23 pm

Stephanie wrote:Hi Tekk3y,

.config and autostart are directories that should already be there. If you open a new terminal window and type:
cd .config (return)
then type:
cd autostart (return)
there should not be any errors, and you should see a prompt that looks like this:
~/.config/autostart $
I tried this again (and I even tried re-installing the OS) but the autostart directory is still not there. When I type cd autostart (return) I get a message saying 'bash: cd: autostart: No such file or directory'.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 2:27 pm

The autostart is not there with a fresh image

type

Code: Select all

mkdir /home/pi/.config
ignore any error
then type

Code: Select all

mkdir /home/pi/.config/autostart
you should not get any error

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
Stephanie
Posts: 13
Joined: Tue Jun 17, 2014 12:03 pm
Location: Ontario, Canada
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 2:39 pm

My apologies, I thought autostart was already there. I don't remember having to add it myself.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Sat Jun 21, 2014 2:41 pm

simplesi wrote:The autostart is not there with a fresh image

type

Code: Select all

mkdir /home/pi/.config
ignore any error
then type

Code: Select all

mkdir /home/pi/.config/autostart
you should not get any error

Simon
I still dont get a terminal with my program running after boot after doing this... Although its removing the contents of my .py file on every start up. :/

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Sat Jun 21, 2014 4:17 pm

...Why is it so complicated to make a python script launch in terminal on boot. I still haven't accomplished this. :(

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 5:13 pm

Its not that complicated but one spilling mistook or one error and it won't work

And there are LOTS of ways to accomplish the same goal.

Linux isn't like Windows - there is no Startup group :(

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Sat Jun 21, 2014 5:36 pm

So at the moment I have found another way in which to do this. This is by using

Code: Select all

nano ~/.bashrc
and then adding my command to run the script at the end of it. This works although only runs the script once I'v clicked to open terminal. Does anyone know of any ways to automatically open terminal on boot?

Thanks, Tekk3y

User avatar
Cancelor
Posts: 751
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start a Python Script from boot

Sat Jun 21, 2014 7:50 pm

I tried the above and although I wasn't loosing the contents of my files I didn't get anything on screen.

I noticed that when I run my Python file:-

Code: Select all

#!/usr/bin/env python3
import time
print ("hello world")
time.sleep(3)
while(True):
    print("Press Cntrl+c to EXIT")
    time.sleep(3)
by double clicking on it's desktop icon, it asks if I want to run in the terminal but nothing happens :(
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Start a Python Script from boot

Sat Jun 21, 2014 11:00 pm

Im starting to get that you don''t just want to run a script on boot - you want to see it display in a terminal window as well - yes?
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Tue Jun 24, 2014 11:33 am

simplesi wrote:Im starting to get that you don''t just want to run a script on boot - you want to see it display in a terminal window as well - yes?
Simon
Yes that is what I wanted it to do. :)

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start a Python Script from boot

Tue Jun 24, 2014 4:05 pm

...So i'v now solved the problem. If anyone else is wanting to do this instead of finding bits and pieces over my two questions im list what i'v done below.

First of all you want your pi to launch straight to the desktop. I did this using Noobs of which you can set this when installing the OS in the options.

Secondly type:

Code: Select all

mkdir ~/.config/autostart
to create the autostart directory.

after that you need to edit a .desktop file in the directory by using the following command.

Code: Select all

nano ~/.config/autostart/myprogram.desktop
Once there you need to place the following text into the .desktop file:

Code: Select all

[Desktop Entry]
Exec= sudo python  /home/pi/Desktop/program.py
Type=Application
note to only use sudo if you need root access and the '/home/pi/Desktop/program.py' section should be the route to your python script.

After this you need to make your python script execuitable and add the orrect hash to the front of the code. For this open your python file in a text editor and add the following to the top of the file.

Code: Select all

#!/usr/bin/python
after this you need to make your script executable, to do this move to the directory your script is in and type teh following into the terminal.

Code: Select all

chmod +x myscript.py
this will be the same except change myscript.py for your script name and extension. After this you should be able to restart your pi and the terminal should launch on boot with your script running.

Return to “Troubleshooting”

Who is online

Users browsing this forum: MirkoKay and 65 guests