I have a bash file I want to run when I turn on the Pi. I have seen a few solutions utilizing init d or adding it to some other file or directory, but the problem is that when I sign in with SSH they run the command again. I only want to run it when the Pi starts up and not everytime I log in. It shouldn't be every time the GUI boots up either, because I use VNC as well. I only want it to execute when the Pi starts up at first. If possible, even before logging in, but if this can't be done, it's not a problem, I can turn on autologin.
1 Answer
As noted in the Raspberry Pi documentation, you can add commands to /etc/rc.local
to run on startup.
Edit it with your favourite editor (note: root is required for this):
$ sudo nano /etc/rc.local
Some tips from the documentation which are worth paying attention to:
Add commands below the comment, but leave the line exit 0 at the end, then save the file and exit.
WARNING
If your command runs continuously (perhaps runs an infinite loop) or is likely not to exit, you must be sure to fork the process by adding an ampersand to the end of the command, like so:
python /home/pi/myscript.py &
It may also be wise to check that the +x
(executable) bit is set on the script, so it actually runs.
$ ls -l /etc/rc.local
-rwxr-xr-x 1 root root 419 2017-01-01 12:34 /etc/rc.local
If the x
bit isn't there, you can set it with:
$ sudo chmod +x /etc/rc.local