[systemd-devel] Question about service start/stop behavior guarantees
Richard Maw
richard.maw at codethink.co.uk
Tue Dec 15 07:02:17 PST 2015
On Mon, Dec 14, 2015 at 10:13:57AM -0800, Eudean Sun wrote:
> I have a service that I'd like to start first among a bunch of services
> when booting and also stop first when the system is reboot or shutdown.
> Originally I was trying to guarantee that the service started/stopped first
> exclusively, but I was having trouble getting that to work so I have
> concluded that it would be acceptable for it to simply start/stop in
> parallel with other services at boot/shutdown. (Note: the exclusive
> start/stop ordering used to be achieved with chkconfig.)
>
> I'm using the following unit file to achieve this behavior:
>
> [Unit]
> Description=myService
>
> [Service]
> Type=oneshot
> RemainAfterExit=true
> ExecStart=/usr/bin/myService start
> ExecStop=/usr/bin/myService stop
>
> [Install]
> WantedBy=multi-user.target
>
> My question is whether I can count on the service being stopped in parallel
> with other services at shutdown. For example, upon reboot, would it be
> possible for systemd to decide to finish stopping some other service first,
> hang, and then never stop myService?
Services are stopped in reverse order to how they are started.
If you don't declare any Before= or After= then it will attempt to stop both in
parallel,
so you shouldn't have a case where it blocks stopping something that was
started in parallel and hangs,
since the other service will be being stopped at the same time.
If the shutdown order is wrong, and you need to stop one service before another,
then systemd will kill the service that blocks on shutdown after a timeout,
so the other service will eventually be stopped.
If the other service doesn't cleanly handle being killed
then you need to provide a safe explicit ordering with Before= and After=.
More information about the systemd-devel
mailing list