You are not logged in.

#1 2014-09-16 19:04:38

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Delayed Hibernation systemd service

Hi,

I'm following these instructions:
https://wiki.archlinux.org/index.php/Po … rvice_file

in order to enable hibernation after a period of being in suspend mode.

My problem is that the hibernate 'decision' seems to be one step behind, ie after a period longer than the alarm's SLEEPLENGTH, my laptop wakes, only to instantly hibernate in front of my eyes.

Obviously this is sub-optimal..

How should I go about identifying what is causing the problem?

Editing the wiki article will be a piece of cake in comparison.

Offline

#2 2014-09-16 20:11:11

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: Delayed Hibernation systemd service

[...], my laptop wakes, only to instantly hibernate in front of my eyes.

If I understand you correctly, then this is what's described in the wiki:

[...] but sets a timer to wake up later to perform hibernation. [...]

What you seem to want would be a combination of "hybrid-sleep" and "delayed hibernation". While this would be a really neat and desirable feature I'm not sure, whether it's possible currently. At least I think it's not what is in the wiki.

What you could do is use hybrid-sleep and after a period of time cut the power manually... Maybe there are other/better ways. I didn't know hybrid-sleep at all until I saw it in the wiki just now.


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#3 2014-09-16 20:14:40

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

No, what I mean is that before I return to the laptop, it should have already gone into hibernation, as the time is well beyond the SLEEPLENGTH.

When I turn it back on, it shouldn't instantly hibernate again. I presumed that the script is checking whether woken because of an alarm (please hibernate) or not (please don't).

It appears that something is not right.

Last edited by alasdairc (2014-09-16 20:14:55)

Offline

#4 2014-09-16 20:16:24

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

So to be clear, the point of the script is to wake up the machine after N minutes, automatically, and hibernate in order to save power and protect against data loss.

Offline

#5 2014-09-16 21:16:49

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: Delayed Hibernation systemd service

Ok. I can't look at that in depth right now, but I can see that:
(from the service file in the wiki, executing in a terminal)

(setting SLEEPLENGHT first)
$ SLEEPLENGTH=+2hour
$ echo -n "alarm set for "; date +%%s -d$SLEEPLENGTH | tee $WAKEALARM
(outputs:)
alarm set for %s
(whereas)
$ echo -n "alarm set for "; date +%s -d$SLEEPLENGTH | tee $WAKEALARM
(outputs:)
alarm set for 1410909035

maybe the additional % is the problem.
Continue debugging like this e.g. setting $WAKEALARM manually, test whether this works etc.


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#6 2014-09-16 22:30:44

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: Delayed Hibernation systemd service

 The extra "%" is an escape symbol for the following "%", since systemd uses its own variables set:

man systemd.unit wrote:

       ┌──────────┬─────────────────────────────────┬─────────────────────────────────┐
       │Specifier │ Meaning                         │ Details                         │
       ├──────────┼─────────────────────────────────┼─────────────────────────────────┤
       │"%n"      │ Full unit name                  │                   
       │"%%"      │ Single percent sign             │ Use "%%" in place of "%" to     │
       │              │                                              │ specify a single percent sign.

 See the specifiers section.

Side note: I am not bothering correcting that format.

Last edited by emeres (2014-09-16 22:31:18)

Offline

#7 2014-09-16 22:47:46

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: Delayed Hibernation systemd service

Ah, well. Sorry, I missed that.
Thanks

Edit: Still this line somehow fails to write to /sys/class/rtc/rtc0/wakealarm correctly on my system.
Whereas e.g. echo `date '+%s' -d '+ 5 minutes'` > /sys/class/rtc/rtc0/wakealarm
(as root) does work...
Edit: Wrong again, I made a mistake, should go to sleep...
(Checking by 'cat /sys/class/rtc/rtc0/wakealarm' , use 'echo 0 > /sys/class/rtc/rtc0/wakealarm' to reset.)

Also found this: http://www.mythtv.org/wiki/ACPI_Wakeup# … Fwakealarm
Seems to include useful info, also about disabling hwclock on shutdown (in case it would be enabled) in order for this feature to work.

Good Luck

Last edited by rebootl (2014-09-16 23:16:56)


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#8 2014-09-17 07:30:55

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

I'll have a look through the mythtv article. Thanks for the help.

Yep the wakealarm is waking the machine after the specified time, however much later on, when I turn it back on manually, the laptop hibernates immediately too.

Offline

#9 2014-09-17 18:41:23

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Delayed Hibernation systemd service

Reminds me of the discussion that we had here: https://bbs.archlinux.org/viewtopic.php … 1#p1430561

This feature would be great to be integrated into systemd directly.

Offline

#10 2014-09-17 18:45:29

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

Great link - I'd missed that. will give it a wee read through

Offline

#11 2014-09-18 14:38:04

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: Delayed Hibernation systemd service

alasdairc wrote:

Yep the wakealarm is waking the machine after the specified time, however much later on, when I turn it back on manually, the laptop hibernates immediately too.

 I skimmed through the first service, but this indicates some of the timers/variables are not reset unless I misunderstood your "when I turn it back on" phrase. I see no gain in putting the script inside the service file. It can be done, but adds unnecessary risk of format errors. Put [another|the] 'echo 0 > $WAKEALARM;' line before 'systemctl hibernate;' to make sure the alarm is cleared or double check before suspending next time. Maybe you want to take a look at rtcwake.

Offline

#12 2014-09-25 11:51:45

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Delayed Hibernation systemd service

Can someone help me?

I created the following file:

~ cat /etc/systemd/system/suspend-to-hibernate.service
[Unit]
Description=Delayed hibernation trigger
Documentation=https://bbs.archlinux.org/viewtopic.php?pid=1420279#p1420279
Documentation=https://wiki.archlinux.org/index.php/Power_management
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true

[Service]
Type=oneshot
RemainAfterExit=yes
Environment="WAKEALARM=/sys/class/rtc/rtc0/wakealarm"
Environment="SLEEPLENGTH=+15mins"
ExecStart=-/usr/bin/sh -c 'echo -n "alarm set for "; date +%%s -d$SLEEPLENGTH | tee $WAKEALARM'
ExecStop=-/usr/bin/sh -c '\
  alarm=$(cat $WAKEALARM); \
  now=$(date +%%s); \
  if [ -z "$alarm" ] || [ "$now" -ge "$alarm" ]; then \
     echo "hibernate triggered"; \
     systemctl hibernate; \
  else \
     echo "normal wakeup"; \
  fi; \
  echo 0 > $WAKEALARM; \
'

[Install]
WantedBy=sleep.target

However, running it does not trigger any action, nor any error:

~ sudo systemctl start suspend-to-hibernate

Running without sudo obviously fails for privilige reasons:

~ systemctl start suspend-to-hibernate 
Failed to start suspend-to-hibernate.service: Access denied

Offline

#13 2014-10-02 18:00:59

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

You don't start it your self, you just enable it. And then it's started/stopped in conjunction with the sleep service.

I'm very slowly debugging my own problem

Offline

#14 2014-10-02 18:19:59

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

I noticed I didn't have NTP enabled, weirdly the computer hadn't drifted a huge amount (few seconds). Have now enabled systemd-timesyncd.

But more relevant - I just saw that my laptop refuses to start from sleep with the lid closed. It's a lenovo X200. The battery light flashes on for a few seconds, then off again after the alarm period. The machine stays in suspend mode, hibernating only after I open the lid manually. So this must be what is causing the issue. The alarm wakes the machine fine if the lid is open.

Offline

#15 2014-10-04 13:37:00

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

I discovered an option in the BIOS which stopped the laptop turning on from RTC alarm while on battery. DIsabling this seems to have improved the situation. The laptop is now going into hibernation mode correctly even with lid closed.

However a couple of times this has failed, leaving the suspend LED indicator (crescent moon) flashing continiously. When this happens I have to hold the power button down to force a power off.

Seems like this is as far as I can get with this so am going to move on and accept what I have.

Offline

#16 2014-10-04 16:58:17

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Delayed Hibernation systemd service

@alasdairc

Do you have a X200 or X200T?

I own the latter and cannot reproduce your problem.

Offline

#17 2014-10-04 16:58:51

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

X200, no T

Offline

#18 2014-10-04 17:01:43

alasdairc
Member
Registered: 2013-10-04
Posts: 16

Re: Delayed Hibernation systemd service

@orschiro -  do you have suspend to hibernate working? Are you doing anything differently to the service on the wiki article?

Offline

#19 2014-10-04 18:25:52

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Delayed Hibernation systemd service

@alasdairc

Yes, it worked for me. I used exactly the service from the Wiki article.

But it did not provide what I precisely wanted. It put my machine into hibernation on every standby. However, I would like to manually trigger it selectively.

Unfortunately I cannot help you with your issue.

Last edited by orschiro (2014-10-04 18:26:06)

Offline

#20 2019-11-10 00:21:20

nicoadamo
Member
From: Chile
Registered: 2011-03-23
Posts: 14

Re: Delayed Hibernation systemd service

All,

With the new systemd structure and options, was anyone able to get suspend-then-hibernate working using only settings in

/etc/systemd/sleep.conf

?

Using the following options in that file leads to the system not being able to suspend at all:

AllowSuspendThenHibernate=yes
SuspendMode=suspend-then-hibernate

Either with SuspendState=suspend-then-hibernate suspension is not enabled.
Being curious I tried SuspendState=mem standby freeze suspend-then-hibernate, but it only leads to suspension in memory and never trying hibernation.


Although the man page

man systemd-sleep.conf

mentions

SuspendMode=, HibernateMode=, HybridSleepMode=
           The string to be written to /sys/power/disk by, respectively, systemd-
           suspend.service(8), systemd-hibernate.service(8), systemd-hybrid-
           sleep.service(8), or systemd-suspend-then-hibernate.service(8). More
           than one value can be specified by separating multiple values with
           whitespace. They will be tried in turn, until one is written without
           error. If neither succeeds, the operation will be aborted.

       SuspendState=, HibernateState=, HybridSleepState=
           The string to be written to /sys/power/state by, respectively, systemd-
           suspend.service(8), systemd-hibernate.service(8), systemd-hybrid-
           sleep.service(8), or systemd-suspend-then-hibernate.service(8). More
           than one value can be specified by separating multiple values with
           whitespace. They will be tried in turn, until one is written without
           error. If neither succeeds, the operation will be aborted.

it seems that suspend-then-hibernate is not a valid value neither for /sys/power/disk nor /sys/power/state
This issue shows an effective solution by symlinking the services:

 ln -s /usr/lib/systemd/system/systemd-suspend-then-hibernate.service /etc/systemd/system/systemd-suspend.service

The same forum mentions that KDE should show a new option for Suspend then hibernate for PowerDevil 5.17, but as of 9-Nov-19 and v5.17.2, it is still not available.

Offline

#21 2019-11-10 12:03:14

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,406

Re: Delayed Hibernation systemd service

Please don't necrobump a 5 year old thread make your own thread if you have a question regarding this and link back here if you feel it to be relevant.

Closing.

Offline

Board footer

Powered by FluxBB