You are not logged in.

#1 2015-07-08 08:13:19

alexspb21
Member
Registered: 2015-07-08
Posts: 3

[systemd] custom service unit not triggered by sleep.target

I'm testing a custom unit file to run some post-resume script, but it seems that the service was not triggered at all.

1. The script: /home/user/bin/test.sh

#!/bin/bash
date >> /home/user/test.log

And made it executable by chmod +x /home/user/bin/test.sh

2. The custom service file: /etc/systemd/system/custom.service

[Unit]
Description=Local system resume actions
After=sleep.target

[Service]
Type=simple
ExecStart=/home/user/bin/test.sh

[Install]
WantedBy=sleep.target

Enabled it by systemctl enable custom.service, and it returns success.

systemctl start custom.service runs the script just fine, a test.log appeared in my home folder which indicates the service ran as expected. And there is corresponding log entries in journalctl -u custom.service.

Check the state by systemctl status custom.service:

● custom.service - Local system resume actions
   Loaded: loaded (/etc/systemd/system/custom.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

Which seems to be A-OK.

But upon waking from suspend, the script was not run, and nothing appeared at journalctl -u custom.service, which means the service was not triggered at all?

I'm running out of ideas, what did I do wrong? sad

Thanks!

Offline

#2 2015-07-08 08:38:35

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: [systemd] custom service unit not triggered by sleep.target

Try the following logic:

Before=sleep.target but After=suspend.target.

Offline

#3 2015-07-08 09:17:54

alexspb21
Member
Registered: 2015-07-08
Posts: 3

Re: [systemd] custom service unit not triggered by sleep.target

Awebb wrote:

Try the following logic:

Before=sleep.target but After=suspend.target.

Thanks.

I changed the unit file to:

[Unit]
Description=Local system resume actions
Before=sleep.target
After=suspend.target
... ...
... ...

then

systemctl daemon-reload
systemctl restart custom.service

pm-suspend

Still not working. sad

Any ideas?

EDIT: I know some workarounds by using pm-utils hooks or systemd-suspend hooks, but I'd like to solve it in the unit files. smile

Last edited by alexspb21 (2015-07-08 09:21:57)

Offline

#4 2015-07-08 09:39:09

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: [systemd] custom service unit not triggered by sleep.target

Yes. You want this thing to run after resume. Have a look at the power management wiki article, especially the "Suspend/resume service files" section.

Offline

#5 2015-07-08 10:21:02

alexspb21
Member
Registered: 2015-07-08
Posts: 3

Re: [systemd] custom service unit not triggered by sleep.target

Ok, now it works, but inconsistently.

Use

 systemctl suspend 

to suspend seems to have triggered the script.

But when I changed

ExecStart=/home/user/bin/test.sh

to

ExecStart=/usr/bin/hdparm -B 254 -S 0 /dev/sda

It didn't work.

Manually run the service seems to work fine again.

Now reading https://wiki.archlinux.org/index.php/Po … vice_files

Offline

Board footer

Powered by FluxBB