I want to have a unit that starts/stops on suspend (but not hibernate). Using WantedBy=sleep.target works for suspend or hibernate, but WantedBy=suspend.target will not work because systemd-suspend.service doesn't have StopWhenUnneeded=yes. Is there any downside to add StopWhenUnneeded to suspend? Is there any other way to have a service stop/stop based on suspend that I'm missing?
Not following here. systemd-suspend.service terminates as soon as the suspend is complete, on its own, because it does exit(0), there's no point of adding StopWhenUnneeded=yes to it. Don't grok the problem. Your service should be able to do BindTo=systemd-suspend.service and Before=systemd-suspend.service. That way, its ExecStart= should be invoked before, and its ExecStop= after the suspend. Also note the hook directory in /usr/lib/systemd/system-sleep/. See systemd-sleep(8) for details.
Looks like BindsTo=systemd-suspend.service and Before=systemd-suspend.service does what I want. I guess my confusion is why does suspend.target stay active after resuming when sleep.target doesn't? I don't think I'm the only one confused by it. The Arch wiki[1] has examples with suspend.target and sleep.target. I don't think the suspend.target ones will work as expected. [1] https://wiki.archlinux.org/index.php/Power_management#Suspend.2Fresume_service_files
Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.