7

I have a Raspberry Pi running Raspbian that I want to auto-login and then automatically run a program that uses the GPIO as root.

I have auto-login working as per this website.

What is the best way to run a program that needs GPIO? Is it adding the pi user to sudoers permissions? Or would adding a line with sudo to the bottom of the ~/.bashrc file? I would guess the second so that the pi user can't do any other damage?

  • 5
    I personally add the command to /etc/rc.local. That way it runs on startup, regardless of whether autologon is enabled or not. (Commands in /etc/rc.local are always run as root, so no need to add sudo). – Gerben Jan 10 '14 at 16:33
5

To run your program just add it to ~/.bashrc using sudo, as the pi user has sudoers permissions without password authentication already, this way the program will be started when you login.

What you can do instead to is create a service unit, is you are using arch on your pi just create a new systemd unit file in /etc/systemd/system and then enable it, the program will be started when the computer is powered on.

0

Adding a sudo in ~/.bashrc should work fine, on a standard Raspbian install the pi user has sudoer permissions already.

Alternatively if it is just a single program you wish to run, you can tell it to always run as root.

You do this by setting the owner to be root, then telling the file it is always loaded as if the owner of the file opened it.

Lets say I have a script called test.sh for example:

sudo chown root test.sh
sudo chmod +s test.sh

The first command makes the owner root, the second command tells it to load as the owner of the file.

If we now run the command normally it should run as the root user regardless of who started it or where (no sudo required).

  • Yes, adding a sudo test.sh in .bashrc worked. For some reason the second two commands didn't seem to, despite the programming running fine after changing to root with sudo su and also being listed as owned by root in ls -l. – mirams Jan 9 '14 at 16:47
  • What permissions did ls -l show (e.g. -rw-r--r--)? – PiBorg Jan 10 '14 at 9:49
  • It shows -rwxr-xr-x 1 root pi – mirams Jan 10 '14 at 14:22
  • The sticky bit is not set (the first symbol should be s not -), maybe I got the command wrong. I have changed the order and syntax to match a setup script I wrote ages ago for something else. – PiBorg Jan 13 '14 at 17:08
  • Still no luck with the new commands I'm afraid – mirams Jan 14 '14 at 16:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.