Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to execute a script when my Raspberry Pi boots up. I would like the web browser to open up automatically.

I have tried to find a simple solution, (like dropping my script in some "startup" directory or something similar) but I am not seeing anything like that.

I have looked into Upstart, but I'm struggling to grasp how that works. Any scripts I've tried have not worked when I test them out.

share|improve this question
3  
What operating system are you running? In FreeBSD, which I'm using on my Pi, you can add startup scripts to /usr/local/etc/rc.d/, or just add things to /etc/rc.local. Different Linux distros have different recommended methods. If you're running X on it, then you should might look into adding things to your .xinitrc or .xsession file. – ghoti Nov 16 '13 at 4:08
    
More information about the X startup process can be found over here. – ghoti Nov 16 '13 at 4:27
up vote 82 down vote accepted

For running Midori on startup, take a look at this tutorial. For DIY solutions, read on.


You can add your script executable command to the bottom of .bashrc that will run your script every time you log in.

  1. Make sure you are in the pi folder:

    $ cd ~
    
  2. Create a file and write a script to run in the file:

    $ sudo nano superscript
    
  3. Save and exit: Ctrl+X, Y, Enter

  4. Open up .bashrc for configuration:

    $ sudo nano .bashrc
    
  5. Scroll down to the bottom and add the line: ./superscript

  6. Save and exit: Ctrl+X, Y, Enter


If you are looking for a solution that works on bootup to the console, take a look at this link. Basic rundown:

  1. Create a file for your startup script and write your script in the file:

    $ sudo nano /etc/init.d/superscript
    
  2. Save and exit: Ctrl+X, Y, Enter

  3. Make the script executable:

    $ sudo chmod 755 /etc/init.d/superscript
    
  4. Register script to be run at startup:

    $ sudo update-rc.d superscript defaults
    

If you want a script to run when you boot into the LXDE environment, you could take a look at this Raspberry Pi forum post:

  1. Navigate to etc/xdg/lxsession/LXDE

  2. Open the autostart file in that folder:

    $ sudo nano autostart
    
  3. Add @midori on a new line. If you want to run something like a python script, put something like @python mypython.py on a new line. Running a script file would be @./superscript, but for some reason the script runs in an infinite loop (perhaps this will stop that).

  4. Save and exit: Ctrl+X, Y, Enter

  5. Restart your Raspberry Pi into the LXDE environment.

share|improve this answer
    
The tutorial on setting up Midori on startup was just what I was looking for. Not sure why there are so many ways to do such a simple thing, but I'm glad it's working now. – Tyler Murry Aug 2 '13 at 1:44
1  
@syb0rg The run at login part works like a charm (+1) if I log in via ssh, but not when the lxde desktop session starts. is there a way to do that ? – George Profenza Sep 13 '13 at 19:50
    
@GeorgeProfenza When you do $ sudo startx? – syb0rg Sep 13 '13 at 23:01
    
@syb0rg Using raspi-config raspian is configured to boot with the graphical interface. I'm not sure how that's done behind the scenes(if sudo startx is executed or simply startx) because I see the graphical interface, but the script does not run. However, when I ssh into the RPi the script starts. – George Profenza Sep 13 '13 at 23:35
2  
Just wanted to point out that the pyhton script will run, but if there are any errors, it will just be somewhere in the background using the /etc/xdg/lxsession/LXDE/autostart method. using .barshrc will reveal errors as well, but it's really important to make sure the script is tight in the first place (found that out the hard way :) ) – George Profenza Sep 25 '13 at 1:23

The way that I've seen most people do it (have a look on the Raspberry Pi forums), and have done myself with success is using /etc/rc.local.

All you need to do here is put ./myscript in the rc.local text file. If it's in python, put python myscript.py.

This literally is "a simple solution, (like dropping my script in some "startup" directory or something similar)"- maybe search on the forums when you're having questions as well, this solution came up on the first 4 results of a google search!

share|improve this answer
4  
FYI the rc.local by default has various comments about the script doing nothing and needing executable bits changed. This is not true just enter the command for your script before the exit 0 and it will run on startup. Make sure your script exits of runs in the background or it will block the login prompt. Yes, thats what I did. – rob Nov 20 '14 at 22:41
    
    
@rob Do you mean they suggest the script to be set as executable? This is mitigated in the question by running the script by the command python myscript.py. If you want to chmod +x it and add #! /bin/python, you can run the script by doing $pathtofile/myscript.py where $pathtofile is . if you're in the same directory or the absolute or relative path to the file. – JFA Oct 29 '15 at 8:46
    
@JFA run "ls -l /etc/rc.local" that will show if that file is executable or not, look for the "x" in the list of permissions. – rob Oct 29 '15 at 21:39
    
@rob yes I understand how that works. I was trying to clarify your post. – JFA Oct 30 '15 at 14:55

Add it to the crontab

The crontab runs commands at defined times.


Edit the file:

sudo crontab -e

Add line to file (here a python script):

@reboot python3 /home/pi/Desktop/exemple.py &
share|improve this answer
    
To be a little nitpicking here, technically it's not crontab that runs the command, but anyways. With regard to the listed line to add, it is advisable to put full paths for the commands defined here (in this case the full path to python3), see here – Ghanima Mar 28 at 20:39

I want to throw in my two cents, even though this is an old question but commonly asked to do simple thing - autostart. I tried all the suggested solutions in all the answers for this question. NONE of them worked for me. I am using Raspberry PI Model 2 with Raspbian.

The only way I could get my application to autostart successfully is through a script as follows. I say successfully because my application started as expected without having any issue like starting with wrong work path.

1.Create an empty file with extension .sh and name it whatever you want.

2.Copy and Paste the following EXACTLY except change "your application name" to the script name that you just created.

 #! /bin/sh

 ### BEGIN INIT INFO
 # Provides:          noip
 # Required-Start:    $remote_fs $syslog
 # Required-Stop:     $remote_fs $syslog
 # Default-Start:     2 3 4 5
 # Default-Stop:      0 1 6
 # Short-Description: Simple script to start a program at boot
 ### END INIT INFO

 #change /direct/path/to/your/application to the path your application is in.
 cd /direct/path/to/your/application      # example cd /home/pi/myprogram/

 #change YourProgramExactName to Exact name of your program that you want to auto start
 ./YourProgramExactName

 exit 0 
  1. Then, save the script file within your application folder

  2. Then, open /home/pi/.config/autostart folder. It might be different in your case. Just open your home folder and enable view hidden folders. open .config/autostart.

  3. within autostart folder you will need to create a shortcut to your script file that you created as follows. Create an empty file with extension .desktop.

  4. Copy and paste the following in the empty desktop file except you will need to change Comment, Name, Exec, Path and Icon field's value.

    [Desktop Entry]
    Comment=
    Exec=/path/to/Your/application/ApplicationName
    Icon=/Path/to/Your/application/Icon/IconName
    Name=YourApplicationEXACTNAME
    Path=/Path/to/Your/Application-ONLY
    Type=Application
    
  5. Save and close the file after changing all the necessary fields. You are done. Just test it out.

share|improve this answer

I also had trouble with this. On the Raspberry Pi3 running Raspbian this is what I did:

  1. Create a startup shell script in your root directory (I named mine "launch"):

sudo leafpad launch.sh

  1. Save the file
  2. Edit the LXDE-pi autostart file

sudo leafpad /home/pi/.config/lxsession/LXDE-pi/autostart

  1. Add this to the bottom of that file

./launch.sh

  1. reboot
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.