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?

syb0rg
  • 8,178
  • 4
  • 38
  • 51
mirams
  • 3,928
  • 4
  • 19
  • 23

2 Answers2

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.

Amedeo
  • 176
  • 3
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).

PiBorg
  • 1,537
  • 10
  • 11