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?

1
  • 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
    Commented Jan 10, 2014 at 16:33

2 Answers 2

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).

6
  • 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
    Commented Jan 9, 2014 at 16:47
  • What permissions did ls -l show (e.g. -rw-r--r--)?
    – PiBorg
    Commented Jan 10, 2014 at 9:49
  • It shows -rwxr-xr-x 1 root pi
    – mirams
    Commented Jan 10, 2014 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
    Commented Jan 13, 2014 at 17:08
  • 1
    You cannot (effectively) setuid on a script. unix.stackexchange.com/a/2910/25985
    – goldilocks
    Commented Sep 25, 2015 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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