How do you run a node-red exec block with root privileges?
I have two python scripts, camera.py and relay.py. I am able to execute camera.py but I am not able to execute relay.py from node-red.
I am able to run the following through the command line without error: "./camera.py" ,"sudo ./camera.py", "sudo ./relay.py 200". I am not able to run "./relay.py 200" because it gives me an execution error. I think this is because it uses SpiDev which usually requires root privileges.
From a node-red exec block, I can exec the following without error: "camera.py" and "sudo camera.py". However, I can't figure out how to execute "relay.py 200".
Here is the relay.py code:
#!/usr/bin/python
import sys
import spidev
hexForm = format(int(sys.argv[1]),'#04x')
spiBus = 0
spiChannel = 0 #SPI Channel (CE0)
spi = spidev.SpiDev()
spi.open(spiBus,spiChannel)
to_send = hexForm
spi.xfer(to_send)
Here is the node-red flow:
[{"id":"b77b1a31.7df568","type":"http in","z":"56302dc4.e31264","name":"RelayRequest","url":"/RelayRequest","method":"post","swaggerDoc":"","x":95,"y":197,"wires":[["78e73048.889a7","702908ee.357d18"]]},{"id":"ef17aada.044c18","type":"http response","z":"56302dc4.e31264","name":"Return","x":797,"y":207,"wires":[]},{"id":"37db2a8.1eeafd6","type":"exec","z":"56302dc4.e31264","command":"sudo python-scripts/relay.py","addpay":true,"append":"","useSpawn":"","timer":"","name":"ExecRelayScript","x":455,"y":250,"wires":[["cf28fa90.0d7c08","ef17aada.044c18"],["9b8b4782.ebc578"],["48066a13.83d884"]]},{"id":"9b8b4782.ebc578","type":"debug","z":"56302dc4.e31264","name":"stderror2","active":true,"console":"false","complete":"payload","x":648,"y":311,"wires":[]},{"id":"cf28fa90.0d7c08","type":"debug","z":"56302dc4.e31264","name":"stdout2","active":true,"console":"false","complete":"payload","x":812,"y":273,"wires":[]},{"id":"48066a13.83d884","type":"debug","z":"56302dc4.e31264","name":"return code2","active":true,"console":"false","complete":"payload","x":684,"y":361,"wires":[]},{"id":"78e73048.889a7","type":"function","z":"56302dc4.e31264","name":"function","func":"if(msg.req.body)\n{\n msg.payload = msg.req.body\n return msg;\n}\n","outputs":1,"noerr":0,"x":263,"y":251,"wires":[["37db2a8.1eeafd6","5f0cf4ac.3cb30c"]]},{"id":"5f0cf4ac.3cb30c","type":"debug","z":"56302dc4.e31264","name":"function out","active":true,"console":"false","complete":"payload","x":470,"y":164,"wires":[]},{"id":"702908ee.357d18","type":"debug","z":"56302dc4.e31264","name":"stdout2","active":true,"console":"false","complete":"req.body","x":301,"y":108,"wires":[]}]