Is it possible to run a Python script so it doesn't exit if Node-RED stops?

You still have to use sudo, but it won't ask for a password.

Ah, then we've talked past each other. I thought it should be possible to run the command without sudo with that code. I already run it like that since my user is in the sudo group, with the authority to do it without a password in the visudo file.

I have found a way that's supposed to work here:

But even after creating the directory /lib/systemd/user I can't get anywhere. I assume that it's lib, since config isn't an existing directory on Raspbian, and the files for the service were under /lib/systemd/system.

Where does that link say anything about that directory?

It doesn't. It says ~/.config/systemd/user/ Under Debian and Ubuntu the folder is /lib/systemd/system/ because the various bin and lib folders have not been merged into a unified /usr/ prefix yet. At least that's what another linux page said. But I got it now, I thought the directory was on the root, and I couldn't find it. I guess ~/ means the home directory for the user. There is no systemd directory, but I'll try making one and test again there.

Edit: Finally found it! In raspbian it's /usr/lib/systemd/user. And now the service works without Sudo. :smiley:

1 Like

The feature that allows you to run it without sudo is that it is created in your home directory with your user permissions, so you have full access to it. Yes, the ~ means your home directory. You may need to create it as you have found out.

It was the wrong directory, as I said above. Also it has to be run with --user at the end of the command instead of sudo at the beginning. I'll update the writeup further up.

Are you saying it did not work if you put it in the directory under ~/.config. Certainly that works on Ubuntu though I have not tried on Raspbian, but I will be surprised if it doesn't. Yes, you do need --user, but putting it under your home directory means you don't need sudo to create it either.

I found this while googling, and it set me on the right track, sort of:

  • /usr/lib/systemd/system/ is meant for packages which want to install unit files, though under Debian and Ubuntu the folder is actually /lib/systemd/system/ because the various bin and lib folders have not been merged into a unified /usr/ prefix yet.

Maybe there are similar differences with Raspbian and Ubuntu?

But now I have another problem, of course. I can't get Node-RED to stop and start the script with the --user switch. Is that because Node-RED is it's own user that I need to install the script for?

Command failed: systemctl restart myscript.service --user Failed to connect to bus: No such file or directory

Did you try it using the directory under .config. Systemctl should look in both.

Look in nodered.service (you know where it is :slight_smile: ) to see what user is specified. If you installed using the script recommended in the docs it will be whichever user ran the script. Whether that is the issue I don't know, I haven't tried to use --user from node-red.

[Edit] Actually /usr/lib/systemd/system/ should also work. /lib/systemd/system is intended for services installed by packages installed using apt (or similar), the one under /usr is for other services manually installed at the system level and the home directory location is for user specific services.

No, that's the thing. The directory wasn't there, and it only surfaced when I tried to enable the service to see if that worked. But putting the script in /home/pi/.config/systemd/user doesn't help. And I can see that Node-RED is installed as the user pi.

Edit: And if I try to use it from Node-RED with sudo it means that I can't stop it manually with --user because it's running with privileges I can't overturn.

Are you in the situation where you can start and stop it using --user when logged in as the pi user, but exactly the same command doesn't work when run with an exec node?

In the error you showed

Should not --user be before the service name?

According to the syntax I found both ways work. And indeed --user both before the service name and at the end does work when logged in as Pi (I always log in to desktop via VNC with a strong password, and that login it's only accessible from my server), but none of them work in the exec node.

How have you installed node-red? Using the script in node red docs? Running on Raspberry Pi : Node-RED ?

Assuming that is how you installed then I think it is an environment issue. The service does not run in the same environment as a logged in user. There are many hits on google for that error. This gives some clues as to the sort of thing that can cause it, but I am out of my depth when it comes to dbus. https://bbs.archlinux.org/viewtopic.php?id=234813

Actually I think I may have started with a Pi image with Node-RED, or used regular apt get and then I used the bash curl script to update it.

Edit: The first thing that's in my bash history, after installing gparted (because I run on USB disks), is that script.

That is fine. The script takes out the apt version anyway.

Well, then it's still a mystery why the --user way works when logged in, and not from Node-RED. Maybe I could make Node-RED run a script that runs the service?

Sorry, I got the answers in some weird sequence. I am totally out of my depth here too, I know very little about Linux at all, only what I need to run the system. But for my part it's no biggie, I can simply use sudo for the service. I was just trying to get it to work this way so that I could finish off the writeup.

You can put the password in the service file, it should only be visible with elevated rights.

Of course, continuing to use the pi user makes a bit of a nonsense of adding security :wink:

Don't worry about it but don't forget about it either. This is really to remind anyone else reading the thread who might not realise.