3

is there a sleep command that works with real date time and ignores suspend?

echo a;sleep 3600;echo b

i work 10mins, suspend the notebook for 2h. When I wake it up from suspend, "b" will only print after 50mins instead of imediately.

an alternative would be to

nA=`date +%s`;nB=$((nA+3600))
while((`date +%s`<nB));do sleep 1;done

But i wonder if there is a command for that? If sleep --usedatetimecheck 3s 3600 (the suspendable sleep would be optional like 3s) existed it would be great.

Btw, a trick i use to detect when the pc wakes up from suspend is sleep 3s, compare datetime of before sleep and if it is more than 9s it guesses it wokeup (could also check for high load average and increase the 9s test).

i use ubuntu.

Ps.: someone could argue that several things would kickin imediately. But that is why it should be optional. Another thing i just thought is if we could send a signal to update all sleep commands like pkill -SIGUSR1 sleep?

1

1 Answer 1

2

Something like sleep --wake-me-from-suspend --realtime doesn't really exist:

Ignoring suspend is the hard part, because during suspend, well, there's no software running on your CPU anymore, in fact, the CPU isn't running, and you need to resort to being woken up by the real-time clock device of your computer. That will require your operating system to arbitrage access to that, because, well, one RTC, many users and programs, you cannot just give one access to it and hope it doesn't demolish what others have done.

So, you need need something that speaks with your OS (Linux, I guess?) and arranges for that RTC to be set.

systemd-timers are the way to go here.

You need two things:

  1. You need to put what you want to be executed into a format that systemd understands. A unit file (a service, to be specific). Read man systemd.service!
  2. You need to install a systemd timer that is able to wake your machine up from suspend. Since that is a very privileged action, you can only do this as superuser-owned timer. See man systemd.timer, and look for WakeSystem=true.

You write your service file to execute what you want to execute. This can be the process you want to kick off (and it more often than not, it is), so you write a service file of the appropriate Type= (often, that's exec). Or, maybe you do something like creating a file, or sending a message to a port, or something, that your own script waits for to happen before continuing.

You write your timer unit so that it starts your service. You use OnCalendar= to give a hard, "wall-clock and paper calendar" time to wake up. Make sure to specify the time in the right timezone! I generally recommend that you use the quite unambiguous datetime format 2023-10-14 11:15:00 UTC.

You put both files (so, say, veganeye.service and veganeye.timer) into the directory for system units (typically, that's /etc/systemd/system/), and start the timer unit (systemctl enable --now veganeye.timer). You can do all of this, writing the two files, and enabling the timer, in your shell script. It's really not much code.

4
  • 3
    You should consider the shorthand: systemd-run --user --on-calendar=... bash -c "echo b >$(tty)"
    – meuh
    Commented Oct 14, 2023 at 9:32
  • @meuh ooooh, I hadn't considered that! Commented Oct 14, 2023 at 9:32
  • @meuh you should really put that in an answer! Commented Oct 14, 2023 at 9:33
  • I think it's just a new command they added, but underneath the mechanism is exactly the one you describe. Not worth an extra answer.
    – meuh
    Commented Oct 14, 2023 at 9:37

You must log in to answer this question.

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