How to restart a systemctl service from within the process

Chris Green cl at isbd.net
Mon Nov 23 13:14:10 UTC 2020


On Mon, Nov 23, 2020 at 12:30:04PM +0000, Colin Law wrote:
> On Mon, 23 Nov 2020 at 12:12, Chris Green <cl at isbd.net> wrote:
> >
> > On Mon, Nov 23, 2020 at 10:33:54AM +0000, Colin Law wrote:
> > > On Mon, 23 Nov 2020 at 10:08, Chris Green <cl at isbd.net> wrote:
> > > >
> > > > On Mon, Nov 23, 2020 at 09:21:54AM +0000, Colin Law wrote:
> > > > > On Sun, 22 Nov 2020 at 15:50, Chris Green <cl at isbd.net> 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 syncthing at chris.service"
> 
> Should that be
> 
> systemctl --user restart syncthing at chris.service
> 
> But my service is not called nodered at me.service, 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 syncthing at chris.service: Unit syncthing at chris.service 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




More information about the ubuntu-users mailing list