First and foremost, let's note that sleep
does not take suspended time into account.
For example, if you execute the command sleep 5m
, then suspend the computer, then 4 minutes later wake it up, then the sleep
command will terminate 9 minutes after you started it. It sleeps for 5 non-suspended minutes.
With this in mind, I don't think you need that hack at all that you're looking for. Your question implied the opposite, incorrect assumption, namely that a sleep 5m
command can sleep through 4 minutes and 59 seconds that the computer spent suspended and 1 second of awake time.
That being said, let's look at one possible way of telling if the computer has been suspended between two moments.
There are a couple of system clocks, as described in the clock_gettime(2)
manual.
One is CLOCK_MONOTONIC
, this does not count suspended time. It's a value in seconds, from some arbitrary starting point, expressing the amount of time the computer spent actually awake.
There is another one, CLOCK_REALTIME
which expresses the real world's time. However, it's prone to sudden jumps if the clock is adjusted, so isn't fully reliable. Instead, let's use CLOCK_BOOTTIME
. This is a value in seconds, from some arbitrary starting point (presumably the boot time of the system), including the time the computer spent suspended.
You can print these two values with commands like
python3 -c 'import time; print(time.clock_gettime(time.CLOCK_MONOTONIC))'
python3 -c 'import time; print(time.clock_gettime(time.CLOCK_BOOTTIME))'
(There's also a simpler syntax to get the monotonic time, I deliberately picked the syntax that works for both kinds of clocks.)
At certain point, query these two values and compute their difference. At a certain other point do this again.
The difference between those two differences gives you the amount of time the computer was suspended in between. Give or take a little measurement error, since the two values can't be queried at the exact same moment.