I'm building a web dashboard for a temperature sensor. The page is served by node.js, and the sensor connected to GPIO is accessed by a Python script, which is run by node.js. Relevant code:

var execSync = require('child_process').execSync;
var result = execSync('./sensor_scripts/current.py');

The problem is that I need root to access GPIO, but I don't want to run the node.js script as root, for obvious reasons.

What I tried, based on this answer:

sudo chown root current.py
sudo chmod +s current.py

But it didn't work. Is it even possible to do what I want?

  • 1
    Is the Python script complicated? If not you could just convert it to use my pigpio which does not require Python to be run as root. If you are using RPi.GPIO you could wait a week or so for a new release which should not require root privileges. – joan Sep 25 '15 at 17:44
  • 1
    "That answer" (w/ chmod +s) is malarky, because you cannot setuid on a script, including python ones, only a binary. That's why the solution there does not work. Besides joan's suggestion, you could also do something along these lines, but that depends on what specific method you are using to access the GPIO pins. – goldilocks Sep 25 '15 at 18:17
  • @joan: My script in itself is not complicated, but it uses Adafruit's DHT library for communication, so I don't think it would be easy to convert. – ofalvai Sep 27 '15 at 20:39
  • @goldilocks: nodejs runs as user "pi", which is already a member of "gpio" (adduser command told me). But thanks for the suggestion. – ofalvai Sep 27 '15 at 20:50
  • I haven't found the Adafruit DHT library to be reliable. Not that it really matters for temperature readings which you can just repeat. pigpio DHT22 code at abyz.co.uk/rpi/pigpio/examples.html#Python_code – joan Sep 27 '15 at 20:58

Your Answer

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

Browse other questions tagged or ask your own question.