10

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:

#!/bin/bash
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

7

[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 Apr 25 '14 at 23:46
  • 5
    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 Nov 30 '15 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 May 1 '17 at 11:18
13

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
5

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

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy