0

I try to give a behavior to a closing lid of a laptop following this rules:

When the lid is closed:

  1. After 1m, it should be screenlocked ;
  2. After 10m, it should be suspended ;
  3. After 15m, it should be shutdown.

So, I make the following script:

#!/bin/bash
 
USER=fauve
 

sleep 1m
# set screensaver
su -c  "$HOME/.local/bin/screenlock" - $USER

# hibernate
sleep 9m
systemctl suspend


sleep 5m
# shutdown
shutdown 0

But as you see, the shutdown comes when the laptop is under suspension, so it will never shutdown.

So how can I program a suspension or shutdown for 5min, and then make the script keep going to shutdown completely the computer?

1 Answer 1

1

in short: you can't. At least not in software. When you put your computer in suspend, it's suspended. Software like sleep (or more precisely, the systick or timer that tells your kernel to wake up the task after a given time) simply won't run.

So, only thing here is a hardware solution. Your laptop needs to have some alarm clock to wake up, and come out of suspend!

Luckily, that's fairly standard;

echo 0 > /sys/class/rtc/rtc0/wakealarm
echo `date '+%s' -d "+ 5 minutes"` > /sys/class/rtc/rtc0/wakealarm
systemctl suspend
systemctl shutdown 

would do that. IF your laptop supports it.

Note that I doubt the sense in this. The way you wrote your script, your laptop always directly shuts down when it comes out of suspend after 5 minutes or longer. I think you want something else. My guess is you'd want to check whether the 5 minutes have actually passed and if they haven't, assume the laptop was manually brought out of suspend and not shut it down. In that case, you'd need to save the pre-suspend time, and compare it to the time after.

Maybe you want systemctl suspend-then-hibernate? That would seem a likely thing. Under the hood, it does the same: rely on the RTC to come out of suspend at some time, then save the system state to disk and power off unless manually woken up before.

2
  • 1. Fortunately, my laptop support wakealarm, I just test it :) 2. my laptop will not shutdown if it detect any activity in the 15min after the lid close because the whole script is interrupted if I activate my laptop. 3. systemctl suspend-then-hibernate will hibernate and not poweroff. But is the battery consumption of hibernate is the same as poweroff ? And how to set time between suspend and hibernate ?
    – fauve
    Feb 26 at 7:10
  • hibernate == poweroff with storage of RAM to disk/ssd and restoration on boot. It is the same power use. Feb 26 at 9:57

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.