0

I'm trying to have a service restart itself on system resume. This is my the .service file:

[Unit]
Description=Set the battery charge threshold
After=multi-user.target
After=sleep.target
StartLimitBurst=0

[Service]
Type=oneshot
Restart=on-failure
ExecStart=/bin/bash -c 'echo 60 > /sys/class/power_supply/BAT1/charge_control_end_threshold'

[Install]
WantedBy=multi-user.target
WantedBy=sleep.target

I've set WantedBy and After to sleep.target with the hopes that this service will be run after sleep.target is run. However, the service is started just before being putting to sleep instead of on resume. Is this a known bug? Or am I doing something wrong?

I'd also like it to run after resuming from suspend (i.e hibernation). But suspend.target pulls in sleep.target, so fixing this issue ought to allow this to on resume-from-suspend.

I know similar questions exist, however none of the solutions there work (specifically setting WantedBy and After).

PS: Please don't recommend using bash scripts placed in specific folders. Those seem too clunky for my use case and I'd like a simpler solution first instead.

0

2 Answers 2

0

Maybe you need something like this:

[Unit]
Description=Set the battery charge threshold
PartOf=sleep.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/bin/bash -c 'echo 60 > /sys/class/power_supply/BAT1/charge_control_end_threshold'

[Install]
WantedBy=sleep.target
0

I've set WantedBy and After to sleep.target with the hopes that this service will be run after sleep.target is run. However, the service is started just before being putting to sleep instead of on resume.

This is normal, as there is nothing that would actually be ordered before "sleep.target". All built-in services which actually trigger the kernel power actions (such as "systemd-suspend.service") only have After=sleep.target, so they run in parallel with your service.

The actual "sleep.target" does nothing by default, and is not involved in actually putting the system to sleep; that's done by the specific "hibernate.target" and "suspend.target" units.

You would need to hook your service directly after those two targets, or the corresponding "systemd-hibernate.service" and "systemd-suspend.service".

1
  • Thanks. Things make a little bit more sense now. I changed the After and WantedBy to suspend.target. It works as long as I use KDE's sleep option from the power menu. Using the power button or the dedicated Fn key on my laptop does not triggeer the service on resume. Adding hibernate.target also triggers the service on resume from hibernate (more important since it's usually after a resume that I really require the service to run) Jul 13, 2021 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .