1

Show a systemd Type=oneshot service which triggers a system power-off. For example this could be used for sigpwr.target, to avoid the problem that sigpwr.target is not started with the job mode replace-irreversible.

Ensure the oneshot service is not killed before it finishes, as this would cause a failure to be shown.

  1. Is there a possible ordering where systemd stops or kills your service before it has finished? What effect will this have, if any?
  2. Will your service work as well if it is started with the job mode replace-irreversible?
1
# trigger-poweroff.service
[Unit]
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl --no-block poweroff

DefaultDependencies=no avoids an implicit Conflicts=shutdown.target. This means it won't matter if the service is started with the job mode replace-irreversible. It also avoids an implicit Wants=sysinit.target; this would be suboptimal e.g. if you wanted to start this service after transitioning to emergency.target.

Before=shutdown.target makes sure the service finishes before systemd-shutdown starts sending signals. If nothing else, this makes the unit simpler to analyze for correctness. We avoid a deadlock by making sure the service does not also wait for the poweroff to complete.

It wouldn't matter if systemd-shutdown killed the systemctl process with SIGTERM. The process would simply terminate with status WIFSIGNAL and a WTERMSIG of SIGTERM. Systemd treats this as a clean, successful exit. See the definition of SuccessExitStatus in man systemd.exec. However, if the kernel mysteriously didn't schedule this initial exit before the systemd DEFAULT_TIMEOUT_USEC, systemd-shutdown would then send the unblockable signal SIGKILL. Systemd would not treat this as a successful exit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.