1

I have a simple Python script that controls three LEDs. I want to limit the sudo command for my user counter (member of group pimem) to just run this script and because it access the GPIOs also allow /dev/mem/. I am connecting to my Pi with tigervnc.

In my /etc/sudoers file I added the following to the end:

#Allow group members to run scripts in dirs of counter
counter ALL=(ALL) NOPASSWD: /usr/sbin/python /home/counter/Desktop/piCam.py

#Allow users of group pimem to access /dev/mem
%pimem ALL=(ALL) /dev/mem/

My script is this:

#!/usr/bin/env python
# ./piCam.py -f 40
import RPi.GPIO as GPIO
import sys
import time

frequency = 50 #Hz
dutyCycle = 10 # 0.0 - 100.0

LED_1 = 11
LED_2 = 13
LED_3 = 15

def mainArgs(argv):
    if len(argv) < 2:
        return -1
    else:
        for i in range(1, len(argv)):
            arg = argv[i]
            if arg == '-f':
                frequency = int(argv[i+1])
                print("f = ", frequency)
            if arg == '-d':
                dutyCycle = int(argv[i+1])
                print("d = ", dutyCycle)

if __name__ == "__main__":
    mainArgs(sys.argv)

    GPIO.setmode(GPIO.BOARD)
    GPIO.cleanup()
    GPIO.setwarnings(True)

    GPIO.setup(LED_1, GPIO.OUT)
    GPIO.setup(LED_2, GPIO.OUT)
    GPIO.setup(LED_3, GPIO.OUT)

    print("Lights on")

    PIN_1 = GPIO.PWM(LED_1, frequency)
    PIN_1.start(dutyCycle)
    PIN_2 = GPIO.PWM(LED_2, frequency)
    PIN_2.start(dutyCycle)
    PIN_3 = GPIO.PWM(LED_3, frequency)
    PIN_3.start(dutyCycle)

    try:
        while True:
            time.sleep(5)
            #Wait for user to take photo
    except KeyboardInterrupt:
        print("Keyboard Interrupt")

    PIN_1.stop()
    PIN_2.stop()
    PIN_3.stop()

    GPIO.cleanup()

    print("Done")

But when running I get the following error:

Sorry, user counter is not allowed to execute './piCam.py' as root on PiCellCounter.

How can I run this script with sudo but not more?

  • If you type in /home/counter/Desktop/piCam.py does that work? – joan Jan 18 '16 at 21:10
  • @joan I have to try tomorrow, I have currently changed to sudo wheel and installing quite a bit – Greeneco Jan 18 '16 at 21:29
0

@Con, just wondering why if you only want root to be able to run the script you can't just use chown to change the ownership of the file. Have I misunderstood your requirement? If not it would be something like:

    sudo chown root:root nameOfFile.py
  • Nowadays there is no need to use sudo to access the GPIO via RPi.GPIO anyhow - as long as Raspbian jessie is being used. – joan Jan 19 '16 at 10:11

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.