[systemd-devel] sometimes systemd takes a very long time to restart service

Andrey Borzenkov arvidjaar at gmail.com
Wed Jan 2 02:14:19 PST 2013


В Wed, 02 Jan 2013 02:28:19 -0700
JB <general at itpsg.com> пишет:

> Here's the service file:
> *******************************************
> [Unit]
> Description=Webrick Test Service
> After=network.target
> 
> [Service]
> Type=forking
> ExecStart=/usr/bin/ruby /home/rtuser/test.rb
> 
> [Install]
> WantedBy=multi-user.target
> *******************************************
> 
> I put this in /etc/systemd/system/webrickd.service
> Then ran systemctl enable webrickd.service

That is redundant. You can start service even if it is "disabled".

> Then ran systemctl --system daemon-reload
> Then ran systemctl start webrickd.service
> 
> Running it from the command line runs as it should.  Web server starts 
> and test.rb is running in the background as a daemon returning me back 
> to the shell.
> 
> But as soon as I try to use systemd, it starts but as soon as the 
> WEBrick::Daemon.start call is made, everything is killed.

That is because of ...

> webrickd.service - Webrick Test Service
>           Loaded: loaded (/etc/systemd/system/webrickd.service)
>           Active: inactive (dead) since Wed, 02 Jan 2013 01:23:01 -0700; 
> 9min ago
>          Process: 1605 ExecStart=/usr/bin/ruby /home/rtuser/test.rb 
> (code=exited, status=0/SUCCESS)
>         Main PID: 1607 (code=exited, status=0/SUCCESS)
                               ^^^^^^
>           CGroup: name=systemd:/system/webrickd.service
> 

You daemon forks too much. This would be a problem with some of
implementations of initscript as well. Even worse, it may sometimes
work due to race condition and sometimes it may fail.

> I'm really trying to understand systemd but it is times like this when 
> do long for the simpler days of init.

initscripts often hide design or programming errors when systemd makes
them obvious. Do not shoot the messenger.

> 
> If someone could tell me the "right way" to make this work with systemd, 
> I would love to use it but I've been at this on and off for weeks and it 
> isn't getting any easier.  From my perspective systemd appears rigid and 
> quite unforgiving.  I can't call it buggy yet because I don't know that 
> I've found any but it sure doesn't seem nearly as simple and easy to get 
> something running at startup as the documentation would have me believe.
> 
> 

Currently systemd assumes "one service - one main process" model. This
process - main process - represents service for systemd. Service is
alive as long as it runs. When process exits (or terminates) systemd
considers service stopped (gracefully or ungracefully - it is another
matter).

Systemd also expects that program does what you say it does. When you
say that service is "simple" it expects that process is started and does
not fork. If you say that service is "forking" it expects that process
forks exactly once and child remains. It does not expect that child
itself forks yet again and exits.

In your case obvious workaround is to declare RemainAfterExit=true. In
this case systemd will not consider service dead as soon as main
process exited as long as it did not fail (i.e. exit code was 0). 

Does your daemon need to keep internal state between different HTTP
requests? If not, you could make it to be socket-activated on demand.


More information about the systemd-devel mailing list