226

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.

syb0rg
  • 8,178
  • 4
  • 38
  • 51
Tyler Murry
  • 2,363
  • 3
  • 14
  • 5

12 Answers12

205

This Answer is obsolete - and contains methods which were WRONG even when it was written.

Unfortunately this still gets flagged as a Suggestion by StackOverflow.

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 open a terminal (or run a new instance of bash).

  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:

.bashrc is NOT intended to run scripts.

It is run each time a non-login interactive shell is started and is used to configure the shell.
~/.bashrc: executed by bash(1) for non-login shells.

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

  2. 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 ~/.config/lxsession/LXDE-pi

  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.

Milliways
  • 62,573
  • 32
  • 113
  • 225
syb0rg
  • 8,178
  • 4
  • 38
  • 51
59

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!

Ghanima
  • 15,958
  • 17
  • 65
  • 125
theo-brown
  • 2,082
  • 1
  • 16
  • 13
40

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 &
Aaron
  • 559
  • 6
  • 8
16

Autostarting xorg apps

If the script you want to start requires an xorg session then you might try following the freedesktop autostart spec which might or might not work depending on which desktop environment you are using.

Alternatively, you can target your specific desktop environment as described at https://wiki.archlinux.org/index.php/autostarting.

Running a script as a systemd service

If your script fits the description of a daemon or a 'service', and your system is running systemd which is the case for raspbian and most modern linuces, then you can configure your script to run as a systemd service — this provides granular control over the lifecycle and execution environment, as well as preconditions for (re)starting the script, such as the network being up and running. It is also possible to configure the service restart in case of failure (Restart=always, and delay between restarting e.g. RestartSec=10).

For system-wide use create your systemd unit file under /etc/systemd/system, e.g. with vim /etc/systemd/system/autossh.service:

[Unit]
Description=Autossh keepalive daemon
## make sure we only start the service after network is up
Wants=network-online.target
After=network.target

[Service]
## use 'Type=forking' if the service backgrounds itself
## other values are Type=simple (default) and Type=oneshot
Type=forking
## here we can set custom environment variables
Environment=AUTOSSH_GATETIME=0
Environment=AUTOSSH_PORT=0
ExecStart=/usr/local/bin/ssh-keep-alive.sh
ExecStop=/usr/bin/killall -9 autossh
### NOTE: you can have multiple `ExecStop` lines
ExecStop=/usr/bin/killall ssh
# don't use 'nobody' if your script needs to access user files
# (if User is not set the service will run as root)
#User=nobody

# Useful during debugging; remove it once the service is working
StandardOutput=console

[Install]
WantedBy=multi-user.target

See also:

Now we are ready to test the service:

systemctl start autossh

Checking the status of the service:

systemctl status autossh

Stopping the service:

systemctl stop autossh

Once you verified that the service works as expected enable it with:

systemctl enable autossh

NOTE: For security purposes systemd will run the script in a restricted environment, similar to how crontab scripts are run, therefore don't make any assumptions about pre-existing system variables. Use the Environment keys if your script needs specific variables to be defined. Adding set -x at the top of your bash script and then running systemctl status my_service might help identify why your script is failing. As a rule of tumb, always use absolute paths for everything including echo and cat, or explicitly define your $PATH.

ccpizza
  • 429
  • 4
  • 9
8

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. If you don't see autostart folder, then create a folder called autostart within .config folder.

  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/Name-of-the-script-file (.sh)
    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.

ThN
  • 1,081
  • 6
  • 22
  • 37
6

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
lamarant
  • 221
  • 2
  • 4
6

NEW METHOD - RASPBERRY PI OS.

This should work on other versions too.

Create a desktop file

xyz.desktop

type the following into it

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=<Application Name Goes here>
Comment=
Exec=  python /home/pi/Desktop/execute_on_boot.py
StartupNotify=false
Terminal=true
Hidden=false

paste this file into the

/home/pi/.config/autostart/

and restart your raspberry pi and it should automatically run your program in a new terminal.

OLD METHOD - RASBIAN STRETCH On the Raspberry Pi3 running Raspbian Stretch this is what I did:

Edit the LXDE-pi autostart file

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

Add this to the bottom of that file

    @sudo python3 /path/to/your/script.py

save & reboot

Amr Sohil
  • 61
  • 1
  • 3
4

Method 1:

To launch a command automatically on login, put the command into a file named

.bashrc

in the user directory (for example /home/pi)

.bashrc is NOT intended to run scripts.

It is run each time a non-login interactive shell is started and is used to configure the shell.
~/.bashrc: executed by bash(1) for non-login shells.

For example, the file could contain

chromium-browser --kiosk www.google.com

to launch Chromium in full-screen pointed to www.google.com


Method 2:

This solution works really well. Once the browser loads there is a small black square in the top left of the screen which seems to be a general bug (its mentioned on forums by others) but otherwise the fullscreen mode hides everything except the browser page.

Edit the autostart file:

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

Comment out everything using a '#' at the start of each line and then add the following lines

Auto run the browser

@xset s off
@xset -dpms
@xset s noblank
@midori -e Fullscreen -a http://google.com

If necessary use the configuration tool to enable auto running of the GUI on powerup

sudo raspi-config

If you need to exit back to the command prompt CTRL + ALT + F1

CTRL + ALT + F2

Milliways
  • 62,573
  • 32
  • 113
  • 225
User98764431
  • 569
  • 1
  • 19
  • 33
0

make a .sh file with the commands 'python /path/to/your/script.py' type 'sudo nano /etc/rc.local' and type the path to the .sh file

before

exit 0

Or you could just type in

crontab -e

or

sudo crontab -e 

if you want the script to run at startup

inside the file type in

@reboot python /path/to/your/script.py &
DS3a
  • 107
  • 8
0

You could place your script at the bottom of /etc/profile file.

Other options did not work for me, but this is maybe because I placed my script on the desktop.

HelpNeeder
  • 178
  • 1
  • 11
-1

it WORKS. (On every re-boot it prepare the following submissions automatic)

$ cat /etc/rc.local
#!/bin/sh -e
echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
echo "1" > /sys/class/gpio/gpio18/value
exit 0
YumYumYum
  • 105
  • 2
-1

This is what I generally do.

  1. Store your file in the raspberry pi home directory. Eg: mycode.py
  2. Edit the file:

    sudo nano .bashrc

.bashrc is NOT intended to run scripts.

It is run each time a non-login interactive shell is started and is used to configure the shell.
~/.bashrc: executed by bash(1) for non-login shells.

  1. Go to the end of the file and write:

    sudo python mycode.py

  2. If you want the output to be stored in a txt file, edit the code in Step 3 as follows:

    sudo python mycode.py >> output.py

Hope this helps!

Milliways
  • 62,573
  • 32
  • 113
  • 225
Doe.John
  • 29
  • 1
  • 1
  • 3