Systemd user service not running ExecStop on poweroff or reboot

I have this user service enabled on my system:

[Unit]
Description=Browser-on-ram
Wants=bor.resync.service
RequiresMountsFor=/home/

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=bor --sync --verbose
ExecStop=bor --unsync --verbose
Slice=background.slice

[Install]
WantedBy=default.target

# vim: ft=systemd

However, it never runs the ExecStop command whenever I shutdown my system graphically or via poweroff. Sometimes it actually does execute ExecStop, I but cannot find a way to reproduce that unfortunately. The service starts up correctly and remains active.

This is just a guess.

What if you add After=bor.resync.service in [Unit]?

That means that there is an explicit order becuase of the After= and that should also mean the shutdown is ordered. I am thinking the lack of an ordering is allow things to stop in a random order and cause your issue.

Problem is that bor-resync.service needs to occur after the main service (the one shown) has started

I think you would need

Before=shutdown.target
Conflicts=shutdown.target

in the [Unit] section. When the shutdown.target is activated, this unit will then be terminated.

The Wants/Requires dependencies do not imply ordering between units.

I see three problems:

  1. in some places you talk about bor.resync.service and in others about bor-resync.service. Please make sure to use a consistent form :wink: The latter is much better, the dot in the name is confusing.
  2. As Villy wrote, you need Before=shutdown.target+Conflicts=shutdown.target to have the unit shut down before the manager goes down.
  3. As Barry mentioned, there is no ordering between the services. Considering that you wrote that “bor-resync.service needs to occur after the main service”, some ordering is likely needed, either Before or After.
1 Like

Looks like I fixed it by changing the other related systemd units. Also bor.resync.service was actually a typo on my part :P. Anyways thanks guys