How to restart a systemctl service from within the process

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

How to restart a systemctl service from within the process

Colin Law
I have a service (a nodejs process running node-red) and want it to
self-restart.  The obvious way appears to be to invoke
systemctl restart theservice
but I am concerned about the fact that the process running the
systemctl command will itself be killed as part of the restart.  It
does seem to work but that is not a guarantee that there is not a
possible race condition that could cause it to fail.  I am hoping that
someone can reassure me that there is not actually a problem doing
this.

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Karl Auer
On Sun, 2020-11-22 at 09:45 +0000, Colin Law wrote:

> I have a service (a nodejs process running node-red) and want it to
> self-restart.  The obvious way appears to be to invoke
> systemctl restart theservice
> but I am concerned about the fact that the process running the
> systemctl command will itself be killed as part of the restart.  It
> does seem to work but that is not a guarantee that there is not a
> possible race condition that could cause it to fail.  I am hoping
> that
> someone can reassure me that there is not actually a problem doing
> this.
>
> Colin
>

Not 100% sure what they are off the top of my head ("Restart=on-
failure" from memory), but there are options you can put in the
.service file that will cause systemd to restart the service if it
exits, along with options to prevent runaway restarts, control how long
before it restarts and so on. There's also an option to just let it die
if it restarts too often within a given period (StartLimitBurst?).ll
That would be my suggestion. You'd probably find the answer in "man
systemd" or related.

Regards, K.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D




--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Karl Auer
In reply to this post by Colin Law
On Sun, 2020-11-22 at 09:45 +0000, Colin Law wrote:
> I have a service (a nodejs process running node-red) and want it to
> self-restart.  The obvious way appears to be to invoke
> systemctl restart theservice

Oh, to add to my previous response (sorry) I mean that to restart the
service from inside the service you simply exit the service, and let
systemd restart it. Not sure if the exit value is relevant.

> but I am concerned about the fact that the process running the
> systemctl command will itself be killed as part of the restart

Pretty sure it won't be. But if you discover that there is a problem,
do let us know :-)

Regards, K.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D




--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Sun, 22 Nov 2020 at 11:14, Karl Auer <[hidden email]> wrote:
>
> On Sun, 2020-11-22 at 09:45 +0000, Colin Law wrote:
> > I have a service (a nodejs process running node-red) and want it to
> > self-restart.  The obvious way appears to be to invoke
> > systemctl restart theservice
>
> Oh, to add to my previous response (sorry) I mean that to restart the
> service from inside the service you simply exit the service, and let
> systemd restart it. Not sure if the exit value is relevant.

Yes it is, that determines whether it is a failure or not and
therefore affects what the restart= option sees.  I am not sure there
is any way of telling node-red to exit, plus I don't really want to
have to override the standard service file for node-red

>
> > but I am concerned about the fact that the process running the
> > systemctl command will itself be killed as part of the restart
>
> Pretty sure it won't be. But if you discover that there is a problem,
> do let us know :-)

I am pretty sure it won't be a problem too, as I hope this is
something that the s/w designers thought of, but there is no way to be
certain of that unless one knows how it works.  I can test it
certainly, but if there is a possible race condition then either it
may fail very occasionally or may not fail at all until I deploy it to
a different system or some system updates or something, so just
because it seems to work does not mean it will always work.

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Karl Auer
On Sun, 2020-11-22 at 11:54 +0000, Colin Law wrote:
> I am not sure there
> is any way of telling node-red to exit

Signal it - KILL, TERM, HUP. To quote Dave Lister (approximately) "one
swift knee in the happy sack and it'll drop like all the rest".

> plus I don't really want to
> have to override the standard service file for node-red

Add a service file for your project and make it depend on node-red?

The main reason I don't like having to run systemctl is that it means
the project has to run as root. Maybe it has to anyway, I know zip
about node-red.

Personally I think adding a single line to the standard service file is
a way neater and cleaner solution than bodgied up calls to systemctl.
And you can automate adding the lie(s) too, and save that with your
project.

Regards, K.


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D




--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Sun, 22 Nov 2020 at 12:43, Karl Auer <[hidden email]> wrote:
>
> On Sun, 2020-11-22 at 11:54 +0000, Colin Law wrote:
> > I am not sure there
> > is any way of telling node-red to exit
>
> Signal it - KILL, TERM, HUP. To quote Dave Lister (approximately) "one
> swift knee in the happy sack and it'll drop like all the rest".

I can't kill it forcibly, as that would close down node-red without
properly closing everything down, writing caches out etc.  If I do a
clean termination then the restart on failure won't restart it.

>
> > plus I don't really want to
> > have to override the standard service file for node-red
>
> Add a service file for your project and make it depend on node-red?

Not necessary, an override file can be added to achieve what is
required.  The problem is that I would have to instruct anyone
importing my code to do that, which is just not appropriate.  They
likely wouldn't have the expertise to do it.

>
> The main reason I don't like having to run systemctl is that it means
> the project has to run as root. Maybe it has to anyway, I know zip
> about node-red.

It doesn't have to run as root.  Ubuntu can be told to allow the
node-red user to use sudo for just that one command without requiring
a password to be entered.  The code would most often be used on a Pi,
probably, and the default there is (sadly) to allow the default user
to use sudo for anything without a password.

>
> Personally I think adding a single line to the standard service file is
> a way neater and cleaner solution than bodgied up calls to systemctl.
> And you can automate adding the lie(s) too, and save that with your
> project.

No I can't for the reasons explained earlier.

What is bodged up about calling systemctl restart?  That is the
recommended way of restarting a service.
You don't need to answer that, I just would like an answer to the
original question.

You say you know nothing about node-red, you should give it a try.  It
is great fun, with an excellent support forum at discourse.nodered.org
For getting started see https://nodered.org/docs/getting-started/

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Karl Auer
On Sun, 2020-11-22 at 13:55 +0000, Colin Law wrote:
> I can't kill it forcibly, as that would close down node-red without
> properly closing everything down, writing caches out etc.

If you use TERM, that will allow it to close down gracefully.

> If I do a
> clean termination then the restart on failure won't restart it.

The Restart option has various possibilities - including "always", so
you it restarts even if it exited cleanly.

> The problem is that I would have to instruct anyone
> importing my code to do that, which is just not appropriate.  They
> likely wouldn't have the expertise to do it.

I think you've painted yourself into the corner of having to use a bad
method just because it is a method that requires no end-user action. If
you can't make any substantive changes then yep, you are stuck with
using systemctl.

> What is bodged up about calling systemctl restart?  That is the
> recommended way of restarting a service.

You have constraints that mean you have to do it that way, so *shrug*.

> You don't need to answer that, I just would like an answer to the
> original question.

I think you know more about that now that anyone except the systemd
developers. I'm sure there is a forum somewhere where they hang out,
maybe ask them?

Regards, K.

PS: If you have any kind of install script for this thing you could
preserve the original service file. Provide an uninstall option that
puts it back.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D




--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Chris Green
In reply to this post by Karl Auer
On Sun, Nov 22, 2020 at 11:42:53PM +1100, Karl Auer wrote:

> On Sun, 2020-11-22 at 11:54 +0000, Colin Law wrote:
> > I am not sure there
> > is any way of telling node-red to exit
>
> Signal it - KILL, TERM, HUP. To quote Dave Lister (approximately) "one
> swift knee in the happy sack and it'll drop like all the rest".
>
> > plus I don't really want to
> > have to override the standard service file for node-red
>
> Add a service file for your project and make it depend on node-red?
>
> The main reason I don't like having to run systemctl is that it means
> the project has to run as root. Maybe it has to anyway, I know zip
> about node-red.
>
You can have user processes run and handled by systemctl.

--
Chris Green

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Watson
In reply to this post by Colin Law
On Sun, Nov 22, 2020 at 09:45:50AM +0000, Colin Law wrote:
> I have a service (a nodejs process running node-red) and want it to
> self-restart.  The obvious way appears to be to invoke
> systemctl restart theservice
> but I am concerned about the fact that the process running the
> systemctl command will itself be killed as part of the restart.  It
> does seem to work but that is not a guarantee that there is not a
> possible race condition that could cause it to fail.  I am hoping that
> someone can reassure me that there is not actually a problem doing
> this.

"systemctl restart" is implemented as sending a single D-Bus method call
(RestartUnit) to pid 1, so I believe this will indeed currently work
since it's pid 1 that's doing the actual restart work and all that the
systemctl process is doing after sending the request is waiting for it
to respond.  I don't think this implementation detail is officially
documented or guaranteed, though, so it might change in future; and
there seems some possibility that error handling might be a bit weird -
for example if attempting to stop the unit manages to kill the systemctl
process but fails to kill the nodejs process, then I don't think your
proposed arrangement would notice that.

If I were implementing this, then my first inclination would be to give
the process a way to re-exec itself, or else fork a new version of
itself and use sd_notify with MAINPID=... to hand over control to it
(see
https://unix.stackexchange.com/questions/202048/self-restarting-service-does-not-restart-with-systemd).
But if that weren't viable for some reason, then I'd be inclined to try
writing a "<your-service-name>-restart.service" unit file with
"Type=oneshot" and "ExecStart=/bin/systemctl restart
<your-service-name>.service", or something along those lines, and then
use "systemctl start <your-service-name>-restart.service".  It's a bit
twistier, and I haven't tested this approach, but I think either of
these approaches would result in more reliable error handling.

--
Colin Watson (he/him)                              [[hidden email]]

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Sun, 22 Nov 2020 at 18:46, Colin Watson <[hidden email]> wrote:
> ...
> "systemctl restart" is implemented as sending a single D-Bus method call
> (RestartUnit) to pid 1, so I believe this will indeed currently work
> since it's pid 1 that's doing the actual restart work and all that the
> systemctl process is doing after sending the request is waiting for it
> to respond.  I don't think this implementation detail is officially
> documented or guaranteed, though, so it might change in future;

Great, thanks very much Colin

Colin L.

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
In reply to this post by Chris Green
On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
>
> You can have user processes run and handled by systemctl.

Does that mean that if I have a service running as a particular user
that the user can 'systemctl restart' it without using sudo?
Searching hasn't found anything relevant, likely because I am not
using the right search terms.


Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Karl Auer
On Mon, 2020-11-23 at 09:21 +0000, Colin Law wrote:
> On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
> > You can have user processes run and handled by systemctl.
>
> Does that mean that if I have a service running as a particular user
> that the user can 'systemctl restart' it without using sudo?
> Searching hasn't found anything relevant, likely because I am not
> using the right search terms.

Interested in the answer. I have a service running as a user "tomcat",
in that the service file has user and group statements, but I can't
manage it other than with sudo.

Someone in this thread said one could manage non-root service units
without sudo; interested in how that works. Not sure it will help you
if node-red has to run as root.

Regards, K.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D




--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Mon, 23 Nov 2020 at 09:41, Karl Auer <[hidden email]> wrote:
> ...
> Someone in this thread said one could manage non-root service units
> without sudo; interested in how that works.

I said you could use sudo for a specific user/command without the need
to enter a password.  Is that what you are referring to?

> Not sure it will help you if node-red has to run as root.

It doesn't have to and it is definitely a bad idea to run node-red as
root.  What gave you that idea?

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Chris Green
In reply to this post by Colin Law
On Mon, Nov 23, 2020 at 09:21:54AM +0000, Colin Law wrote:
> On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
> >
> > You can have user processes run and handled by systemctl.
>
> Does that mean that if I have a service running as a particular user
> that the user can 'systemctl restart' it without using sudo?
> Searching hasn't found anything relevant, likely because I am not
> using the right search terms.
>
Yes. I run syncthing using systemd.

The configuration file for this is:-

    /etc/systemd/system/multi-user.target.wants/[hidden email]

Which is:-

    [Unit]
    Description=Syncthing - Open Source Continuous File Synchronization for %I
    Documentation=man:syncthing(1)
    After=network.target

    [Service]
    User=%i
    ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0
    Restart=on-failure
    RestartSec=5
    SuccessExitStatus=3 4
    RestartForceExitStatus=3 4

    # Hardening
    ProtectSystem=full
    PrivateTmp=true
    SystemCallArchitectures=native
    MemoryDenyWriteExecute=true
    NoNewPrivileges=true

    [Install]
    WantedBy=multi-user.target


There are a number of configuration files for various 'user' services
to be found in:-
    /usr/lib/systemd/system/multi-user.target.wants

--
Chris Green

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Mon, 23 Nov 2020 at 10:08, Chris Green <[hidden email]> wrote:

>
> On Mon, Nov 23, 2020 at 09:21:54AM +0000, Colin Law wrote:
> > On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
> > >
> > > You can have user processes run and handled by systemctl.
> >
> > Does that mean that if I have a service running as a particular user
> > that the user can 'systemctl restart' it without using sudo?
> > Searching hasn't found anything relevant, likely because I am not
> > using the right search terms.
> >
> Yes. I run syncthing using systemd.

Sorry, we are at cross purposes I think.  I am aware that a service
can be run as a user (I am doing that already), the question is
whether that user can somehow run 'systemctl restart' to restart it
without having to use sudo.  It is arguable that that should be
possible, since it is his process.

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Karl Auer
In reply to this post by Colin Law
On Mon, 2020-11-23 at 09:51 +0000, Colin Law wrote:
> On Mon, 23 Nov 2020 at 09:41, Karl Auer <[hidden email]> wrote:
> > Not sure it will help you if node-red has to run as root.
>
> It doesn't have to and it is definitely a bad idea to run node-red as
> root.  What gave you that idea?

I didn't say it did had to. I just thought that if it did, then having
your service be restartable by a non-root user would probably not be
much help, because the underlying service would still not be
restartable without sudo.

I'm guessing that the node-red service file has user and group lines in
it, so that the service runs as that user and group. That being so it
is possible for that user to kill the service - not via systemctl,
which will need sudo, but via a signal. And depending on the signal,
systemctl would then restart the service for you.

It just seems a tad Rube Goldbergish (or Heath Robinsonish if you are
from the other side of the Atlantic) to give a non-root user a sudoers
entry just so that it is able to use sudo to run systemctl to restart
some service. And it doesn't seem any less difficult a thing for the
unskilled user to accomplish than adding a line to a service file.

But I am keenly aware that I probably do not have all the facts. If you
have something that's working, that's the main thing.

Regards, K.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D




--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Chris Green
In reply to this post by Colin Law
On Mon, Nov 23, 2020 at 10:33:54AM +0000, Colin Law wrote:

> On Mon, 23 Nov 2020 at 10:08, Chris Green <[hidden email]> wrote:
> >
> > On Mon, Nov 23, 2020 at 09:21:54AM +0000, Colin Law wrote:
> > > On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
> > > >
> > > > You can have user processes run and handled by systemctl.
> > >
> > > Does that mean that if I have a service running as a particular user
> > > that the user can 'systemctl restart' it without using sudo?
> > > Searching hasn't found anything relevant, likely because I am not
> > > using the right search terms.
> > >
> > Yes. I run syncthing using systemd.
>
> Sorry, we are at cross purposes I think.  I am aware that a service
> can be run as a user (I am doing that already), the question is
> whether that user can somehow run 'systemctl restart' to restart it
> without having to use sudo.  It is arguable that that should be
> possible, since it is his process.
>

"systemctl restart [hidden email]"


--
Chris Green

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Mon, 23 Nov 2020 at 12:12, Chris Green <[hidden email]> wrote:

>
> On Mon, Nov 23, 2020 at 10:33:54AM +0000, Colin Law wrote:
> > On Mon, 23 Nov 2020 at 10:08, Chris Green <[hidden email]> wrote:
> > >
> > > On Mon, Nov 23, 2020 at 09:21:54AM +0000, Colin Law wrote:
> > > > On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
> > > > >
> > > > > You can have user processes run and handled by systemctl.
> > > >
> > > > Does that mean that if I have a service running as a particular user
> > > > that the user can 'systemctl restart' it without using sudo?
> > > > Searching hasn't found anything relevant, likely because I am not
> > > > using the right search terms.
> > > >
> > > Yes. I run syncthing using systemd.
> >
> > Sorry, we are at cross purposes I think.  I am aware that a service
> > can be run as a user (I am doing that already), the question is
> > whether that user can somehow run 'systemctl restart' to restart it
> > without having to use sudo.  It is arguable that that should be
> > possible, since it is his process.
> >
>
> "systemctl restart [hidden email]"

Should that be

systemctl --user restart [hidden email]

But my service is not called [hidden email], it is nodered.service

Description=Node-RED graphical event wiring tool
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
User=me
Group=me
WorkingDirectory=/home/me

Nice=5
Environment="NODE_OPTIONS=--max_old_space_size=512"
ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
# Tag things in the log
SyslogIdentifier=Node-RED

[Install]
WantedBy=multi-user.target

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Chris Green
On Mon, Nov 23, 2020 at 12:30:04PM +0000, Colin Law wrote:

> On Mon, 23 Nov 2020 at 12:12, Chris Green <[hidden email]> wrote:
> >
> > On Mon, Nov 23, 2020 at 10:33:54AM +0000, Colin Law wrote:
> > > On Mon, 23 Nov 2020 at 10:08, Chris Green <[hidden email]> wrote:
> > > >
> > > > On Mon, Nov 23, 2020 at 09:21:54AM +0000, Colin Law wrote:
> > > > > On Sun, 22 Nov 2020 at 15:50, Chris Green <[hidden email]> wrote:
> > > > > >
> > > > > > You can have user processes run and handled by systemctl.
> > > > >
> > > > > Does that mean that if I have a service running as a particular user
> > > > > that the user can 'systemctl restart' it without using sudo?
> > > > > Searching hasn't found anything relevant, likely because I am not
> > > > > using the right search terms.
> > > > >
> > > > Yes. I run syncthing using systemd.
> > >
> > > Sorry, we are at cross purposes I think.  I am aware that a service
> > > can be run as a user (I am doing that already), the question is
> > > whether that user can somehow run 'systemctl restart' to restart it
> > > without having to use sudo.  It is arguable that that should be
> > > possible, since it is his process.
> > >
> >
> > "systemctl restart [hidden email]"
>
> Should that be
>
> systemctl --user restart [hidden email]
>
> But my service is not called [hidden email], it is nodered.service
>
> Description=Node-RED graphical event wiring tool
> Wants=network.target
> Documentation=http://nodered.org/docs/hardware/raspberrypi.html
>
> [Service]
> Type=simple
> User=me
> Group=me
> WorkingDirectory=/home/me
>
> Nice=5
> Environment="NODE_OPTIONS=--max_old_space_size=512"
> ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
> # Use SIGINT to stop
> KillSignal=SIGINT
> # Auto restart on crash
> Restart=on-failure
> # Tag things in the log
> SyslogIdentifier=Node-RED
>
> [Install]
> WantedBy=multi-user.target
>
It's all a bit of a can of worms, adding --user to the line I sent you
simply returns an error:-

    Failed to restart [hidden email]: Unit [hidden email] not found

However I'm sure what you want to do *can* be done.  This thread on
StackExchange is illuminating:-

    https://unix.stackexchange.com/questions/496982/restarting-systemd-service-only-as-a-specific-user#497011

Scroll down to "A third option" in the answers.

--
Chris Green

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: How to restart a systemctl service from within the process

Colin Law
On Mon, 23 Nov 2020 at 13:15, Chris Green <[hidden email]> wrote:

> ...
> It's all a bit of a can of worms, adding --user to the line I sent you
> simply returns an error:-
>
>     Failed to restart [hidden email]: Unit [hidden email] not found
>
> However I'm sure what you want to do *can* be done.  This thread on
> StackExchange is illuminating:-
>
>     https://unix.stackexchange.com/questions/496982/restarting-systemd-service-only-as-a-specific-user#497011
>
> Scroll down to "A third option" in the answers.

Yes, that would work, but again not with the standard node-red
install, which provides the service file.  So I think I am stuck with
the sudo-without-password route which does involve the user doing a
small amount of configuring of the system, but it is minimal, and
won't be messed up by an upgrade to node-red which overwrites the
service file.  For the record the sudo route is achieved by running
sudo visudo and adding

theuser ALL= NOPASSWD: /bin/systemctl restart nodered.service

Thanks all for the help

Colin

>
> --
> Chris Green
>
> --
> ubuntu-users mailing list
> [hidden email]
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users