I know this question has been asked before, but I can't find a fix for my problem, I just can't get my script to run on startup. Here's my script:

iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT 
iptables -A OUTPUT -m state --state NEW -p tcp --sport 22 -j ACCEPT

I put it into the /etc/init.d directory

Made sure it had executable by using chmod 755 /etc/init.d/iptables.sh

And done touch /etc/rc3.d/S01iptables.sh

But nothing :( ... Any suggestions?

[ Nov 2017 ] The accepted answer here should be considered obsolete, please see https://raspberrypi.stackexchange.com/a/75057/5538

5 Answers 5


[Please see the note in the question regarding the obsolescence of this answer.]

Try using this command to ensure your script is added to the boot sequence:

sudo update-rc.d /etc/init.d/nameofscript.sh defaults

Note that you can make a script executable using the +x option with chmod:

chmod +x /etc/init.d/nameofscript.sh
  • 1
    Yeah the update-rc.d command worked :D ... I used it before, but it appeared to just return an error, so I ignored it Lol ... Thanks for the help :D
    – anon
    Commented Apr 25, 2014 at 23:46
  • 7
    It would be sudo update-rc.d nameofscript.sh defaults, at least on Jessie, to avoid update-rc.d: error: initscript does not exist: /etc/init.d//etc/init.d/nameofscript.sh.
    – derFunk
    Commented Nov 30, 2015 at 17:05
  • Does this script have to end within a reasonable time? Or can it be used to loop a video for infinity for example?
    – clankill3r
    Commented May 1, 2017 at 11:18

You could also set up a cronjob (see tutorial) to make it run at startup

(sudo) crontab -e

only add sudo if your script needs superuser privileges. Then add this to your crontab:

@reboot /path/to/script.sh
  • I used this method to launch a script and it shutdown all ssh access for some reason. I had to mount the drive on another linux device and comment out the crontab line. Commented Jan 6, 2020 at 18:41

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

  2. Open the autostart file in that folder:

    $ sudo nano autostart
  3. Add @bash /path/to/scriptname & 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.

taken from Execute script on start-up


I usually just add the line to execute the script in /etc/rc.local, e.g. /path/to/my/script.sh &, before the last line that has exit 0. I added the "&" in order to send the script to the background in case it does not return, otherwise the next commands would not be executed.

So in your case I would simply add the two lines

iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT 
iptables -A OUTPUT -m state --state NEW -p tcp --sport 22 -j ACCEPT

at the end of /etc/rc.local just before exit 0.


first of all create the script for me it's startup_file.sh

move startup_file.sh to /etc/init.d/

make sure it is executable by : sudo chmod +x /etc/init.d/startup_file.sh

now use this command: sudo update-rc.d startup_file.sh defaults

this one was suggested by goldilocks:

sudo update-rc.d /etc/init.d/startup_file.sh defaults ; it didn't work for me !!

just try one of the two should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.