3

I have a Raspberry Pi 2B with Raspbian Wheezy on it. I want it to auto run a program (startup.sh) as root so I can use the GPIO pins. I've tried to use ~/.bashrc but it takes a long time to start or doesn't start at all. When I open LXTeminal it doesn't say pi@raspberrypi ~ $.

In bashrc it says:

sudo sh startup.sh

Is there another way or am I doing something wrong?

Nielske62
  • 45
  • 1
  • 8
  • Start your program from /etc/rc.local. – nsilent22 Jan 24 '16 at 16:01
  • 2
    I don't know where online people are picking up the idea that .bashrc is the thing to use for this, but when I find out I will hunt down those responsible and destroy them forever. – goldilocks Jan 24 '16 at 16:14
  • Welcome to the Raspberry Pi flavoured bit of the Stack Exchange communities! @goldilocks That sound like a response from someone who wishes to arm bears (given your icon that is). 8-P Perhaps we should point out to the OP that ~/.bashrc is run for the user as an initialization file for interactive logins unless overridden by --rcfile (to run a different file) or --noprofile or --norc options to the bash shell - which ignores it. This can have unintentional side effects...! – SlySven Jan 24 '16 at 16:23

1 Answers1

3

As you are using Raspbian Wheezy I am assuming you are using the default init system provided by systen V init rather than the systemd that is used in Jessie and most other GNU/Linux distributions these days.

That being the case you will want to check out putting the name of your script startup.sh & at the end of /etc/rc.local.

This file is executed during startup (make sure your file is owned by and in group root {sudo chown root:root startup.sh} and marked as executable {sudo chmod ug+x startup.sh}), is given with a full path name if not in the /etc directory and, to ensure it is run as a shell script, ensure the first line starts with #!/bin/sh and to ensure it runs in the background and the system continues to boot after running it if it does NOT exit straight away you must do something like using the & backgrounding option on the end of that line in the /etc/local file.

That last bit is important as the system will wait for your script to finish before continuing - which will make it impossible to log in normally otherwise!

SlySven
  • 3,621
  • 1
  • 18
  • 45
  • startup.sh shouldn't start in the background but the system needs to boot after running the file. – Nielske62 Jan 25 '16 at 15:15
  • What should I write in rc.local? I've tried /home/pi/PiClock/startup.sh – Nielske62 Jan 25 '16 at 15:31
  • You need that & on the end: /home/pi/PiClock/startup.sh &; though if your /home is a separate partition (not that common on an RPi but possible on other larger systems) you should not be keeping "system" script files in your personal user directory - but instead should be keeping them somewhere like /usr/local/sbin so if /home fails there won't be extra, unnecessary, issues in starting the system, IMHO...! – SlySven Mar 21 '16 at 12:59