I put my laptop to suspend mode, woke it up later, and now it shows uptime some 23 hours (with uptime), which is obviously not true. I suspect uptime simply returns the difference between the timestamp at boot and now.

Is there a way to show uptime excluding time spent in low power modes like suspend and hibernate?


The following commands work with Python 2.7.15rc1 on my Mint 19.
They will display the uptime excluding sleep time.

python3 -c 'import time;print(f" {(time.clock_gettime(time.CLOCK_MONOTONIC))}")'

The above shows the time in seconds with a decimal.

python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));print(f" {second} seconds")'

The above shows the time in seconds with no decimal.

python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));minute=second//60;print(f" {minute} minutes")'

The above shows the time in minutes.

python3 -c 'import time;s=int(time.clock_gettime(time.CLOCK_MONOTONIC));h=s//3600;m=(s-h*3600)//60;print(f"{h} hours {m} minutes")'

The above shows the time in hours and minutes.

python3 -c 'import time,datetime;print(datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC)))'

The above shows the time in hours, minutes, and seconds.

python3 -c 'import time,datetime;d=datetime.datetime(1,1,1)+datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC));print(f"{d.day-1} days, {d.hour} hours, {d.minute} minutes")'

The above shows the time in days, hours, and minutes.

I have used CLOCK_MONOTONIC and Awk to create a script that will calculate the total time if my Mint has been started more than once on the same date. The Awk command can even be used to calculate the total time in one week/month/year.


You probably want to use the C function clock_gettime(CLOCK_MONOTONIC_RAW), which isn't presented by any utility (that I know of). I see an answer that gives a python example on how to access it, which might be easier than compiling a C program to do the same.

  • As said in the man page, the starting point of the clock is unspecified. To get the uptime, one would need to have the starting point at the boot. – vinc17 Dec 7 '14 at 23:58
  • I've heard that the monotonic clock gets initialized with the hardware time at the moment from which uptime is counted. Then the clock may diverge. I have no evidence for that right now: the only thing to rely on is that CLOCK_MONOTONIC won't jump back. – Tomasz Gandor Mar 17 '16 at 9:23

Parsing Systemd's journalctl

Subtracting the starting time of systemd's suspend.service from the start time (when it was resumed) gives the amount of time suspended:

$ journalctl -b-0 | grep -E 'systemd\[1]: Start.*Suspend'
Oct 31 05:55:19 alien systemd[1]: Starting Suspend...
Oct 31 16:54:26 alien systemd[1]: Started Suspend.
Oct 31 23:21:21 alien systemd[1]: Starting Suspend...
Nov 01 04:29:12 alien systemd[1]: Started Suspend.
 (... SNIP ...)
Nov 07 05:53:36 alien systemd[1]: Starting Suspend...
Nov 07 16:37:36 alien systemd[1]: Started Suspend.
Nov 07 21:07:28 alien systemd[1]: Starting Suspend...
Nov 08 05:08:52 alien systemd[1]: Started Suspend.

The entries marked Started Suspend. can be interpreted as the time the machine was resumed from suspend.

You can get formatted output with this script:

This is what the bash script generates:

$ suspendtime
Oct 31 05:55:19 to Oct 31 16:54:26 lasting 39,547 seconds
Oct 31 23:21:21 to Nov 01 04:29:12 lasting 18,471 seconds
  (... SNIP ...)
Nov 07 05:53:36 to Nov 07 16:37:36 lasting 38,640 seconds
Nov 07 21:07:28 to Nov 08 05:08:52 lasting 28,884 seconds

Linux uptime 693,600 seconds (8 days, 40 minutes)
16 Suspends 457,547 seconds (5 days, 7 hours, 5 minutes, 47 seconds)
Real uptime 236,053 seconds (2 days, 17 hours, 34 minutes, 13 seconds)

Linux reports uptime as 8 days and 40 minutes. Taking into account time suspended, it is really 2 days and about 18 hours.

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.