Start a system command in exec node in root mode

Hello,

i was searching the forum and found some discussion about exec node and the usage of sudo but that unfortunately didn't help me with my issue:

I have the need to call a command in "sudo su" or root mode within exec node of node red.

Details:
I have a 5'' DSI TFT touch display from Waveshare on my raspberry pi, for which i want to control the brightness out of node red.

For controlling the brightness Waveshare proposes a call "echo xxx > /sys/class/backlight//brightness" in root mode, with "xxx" reflecting the level of brightness 0...255.

Generating different levels of brightness, depending on dashboard state - no issue.
Transferring the brightness level in node red to a system call in "sudo su" mode - stuck.

If i try to access the target file from command line in normal user mode or with "sudo" i get a permission denied error. The same is true, when calling it like this within the exec-node.

I already found out that root mode can be achieved with "sudo su" in command line. If i switch to root mode with "sudo su "first and then call "echo xxx > /sys/class/backlight//brightness" it works as expected.

Unfortunately i have no clue, how to achieve this within an exec node or node red. I tried "sudo su && echo" as command already, but didn't succeed.

Any idea how i could solve the challenge ? I am really stuck.

Thank You for any input !
Chris

Hello @chri-kai-in,

Not really a Node-red issue but still...

I believe the problem is that if you do sudo echo hello > filename, the echo command is executed as root but the redirection (the > ) is not.

What happens if you run this from the command line?
echo xxx | sudo tee /sys/class/backlight/brightness
(Obviously use a sensible value instead of "xxx")

If it works, try it via the exec mode.

Hello @jbudd,

Thank You for your quick reply. Your idea works on command line. (i missed a path level in my first unsuccessful tests...). Looks like a more elegant solution than mine below.

I think i found a solution meanwhile myself. So we have two solutions to choose from:

Option 1:

echo "echo xxx > /sys/class/backlight/<display-id>/brightness" | sudo su

In the exec node i distribute the command like the following:

Command: echo "echo
Append: activate msg.payload
Additional parameters: > /sys/class/backlight/<display-id>/brightness | sudo su

Option 2:

echo xxx | sudo tee /sys/class/backlight/<display-id>/brightness

Command: echo
Append: activate msg.payload
Additional parameters: | sudo tee /sys/class/backlight/<display-id>/brightness

Both options work, whereas i find @jbudd s solution much more elegant.

So my issue seems solved to me. Hope it helps others with similar challenges.

BR
Chris

Ok, that's an ingenious approach.
I do wonder if the root shell will be left hanging after it's written to the file.

I almost never use sudo su, and definitely not for a background process, it just feels wrong.

By the way, I notice that in your original post your command was
echo xxx > /sys/class/backlight//brightness
So I suggested
echo xxx | sudo tee /sys/class/backlight/brightness
But now your command is
echo xxx > /sys/class/backlight/display-id/brightness
So did you test my version with the display id ?

Edit, the forum removed the angle brackets round displayid, so we may have been confusing one another

That's an issue on my side with < > brackets need to be prefixed with an "\" to not be parsed in the forum.

An yes my 2nd successful test with the full path did show the expected behavior.

Best Regards
Chris

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.