How to find the uptime since last wake from standby

31

9

I want to know the uptime since the last wake from standby.

The command uptime only shows the difference between current time minus the last startup time.

debian user

Posted 2011-11-14T13:43:54.920

Reputation: 311

Answers

30

In /var/log/pm-suspend.log, look for the last line looking like this one:

Sun Dec 16 09:30:31 CET 2012: Awake.

That's your last wakeup time. You can calculate your uptime since then the way Paul suggested.

Periodically your logrotate will "rotate" logs to prevent them from growing too big, so you may find an empty pm-suspend.log file. In this case, just look for the pm-suspend.log.1 file (you may find also other log files named like pm-suspend.log.2.gz and so on; you can examine them using zcat or zless).

steps

Posted 2011-11-14T13:43:54.920

Reputation: 401

this worked for me – Jacek Pietal – 2014-06-26T15:58:06.023

5What if pm-suspend.log is empty? :( – cprn – 2015-10-27T12:50:56.510

1If you also care about suspend timestamp, use: cat /var/log/pm-suspend.log /var/log/pm-suspend.log.1 | grep -B1 Awake; echo "--"; zcat /var/log/pm-suspend.log.*.gz | grep -B1 Awake – webbertiger – 2016-11-05T00:33:16.773

2No such file in my computer (running Ubuntu 16.04 LTS) – Ramon Suarez – 2017-10-01T13:46:36.113

File is present only if you installed the pm-suspend. But for example Kubuntu goes to suspended state also after I close the notebook. Then the pm-suspend file is empty. – dmatej – 2017-10-03T11:51:42.887

16

For desktops/servers running systemd, while there is no direct command that will tell the info directly (as far as I am aware), all the data is captured in the journal.

You can grep the journal, for example:

echo ">> [SUSPEND] Times during current boot"
journalctl -b 0 |grep "]: Suspending system..."
echo ">> [WAKE] Times during current boot"
journalctl -b 0 |grep "PM: Finishing wakeup"

Or, for fancy output, I wrote a python3 script (runs fine on Fedora 23) Sample output:

Initial Boot Timestamp:  2016-01-15 09:31:32 

     Wake Timestamp     |    Suspend Timestamp   |       Awake Time       |
  --------------------  |  --------------------  |  --------------------  |
   2016-01-15 09:31:32  |   2016-01-15 09:36:03  |          0h  4m        |
   2016-01-15 09:36:22  |   2016-01-15 19:15:04  |          9h 38m        |
   2016-01-15 19:22:21  |   2016-01-15 20:00:05  |          0h 37m        |
   ...
   -------------------  |  --------------------  |  --------------------  | 

Summary: Days Since Boot [8.23] | Days Awake [4.14] | Suspend/Wake Cycles: [28]

The script is in github. link to github repo

Ari

Posted 2011-11-14T13:43:54.920

Reputation: 311

1This worked for me on Ubuntu 16.04 – raphinesse – 2016-05-10T12:46:16.860

Good to know that we can check the journal. – Shiplu Mokaddim – 2016-08-31T12:07:04.873

Use journalctl -b 0 -o short-iso MESSAGE="PM: Finishing wakeup." | tail -1 | cut -d" " -f1 for just the time of the last wakeup – raphinesse – 2016-10-18T17:10:48.567

13

The pm-suspend program is not the only option how to suspend the computer. My log of this program is now empty, but I have found more reliable command:

cat /var/log/syslog | grep 'systemd-sleep' | grep "Suspending\|resumed"

And the output is:

Oct  2 09:11:48 dmatej-lenovo systemd-sleep[931]: Suspending system...
Oct  2 09:53:10 dmatej-lenovo systemd-sleep[931]: System resumed.
Oct  2 15:02:48 dmatej-lenovo systemd-sleep[27516]: Suspending system...
Oct  2 16:07:19 dmatej-lenovo systemd-sleep[27516]: System resumed.
Oct  2 16:32:48 dmatej-lenovo systemd-sleep[29622]: Suspending system...
Oct  2 17:16:41 dmatej-lenovo systemd-sleep[29622]: System resumed.
Oct  3 00:24:58 dmatej-lenovo systemd-sleep[21316]: Suspending system...
Oct  3 08:17:22 dmatej-lenovo systemd-sleep[21316]: System resumed.
Oct  3 09:09:25 dmatej-lenovo systemd-sleep[24739]: Suspending system...
Oct  3 09:50:47 dmatej-lenovo systemd-sleep[24739]: System resumed.

dmatej

Posted 2011-11-14T13:43:54.920

Reputation: 231

2

I did not have pm-suspend.log on my machine.

This worked for me:

/usr/bin/pmset -g log | grep Wake | grep "due to" | tail -n1

Also says what woke the computer up. :-)

Mike

Posted 2011-11-14T13:43:54.920

Reputation: 21

1What if there's no command pmset found and no such file as pmset and pm-suspend.log is empty? :( – cprn – 2015-10-27T12:51:58.680

pm-suspend.log was missing and this works for me (on my iMac) – dayuloli – 2016-05-07T13:05:56.923

2This is for Mac only – plaisthos – 2017-12-31T14:46:09.273

show perfectly in MacOs Catalina – jeff_drumgod – 2019-12-11T23:41:04.237

1

None of these answers worked for me. But I usefully found sleep.target which is made for exactly this:

$ journalctl -n4 -u sleep.target
nov. 17 17:16:37 kaa systemd[1]: Reached target Sleep.
nov. 17 18:46:22 kaa systemd[1]: Stopped target Sleep.
nov. 17 19:27:31 kaa systemd[1]: Reached target Sleep.
nov. 17 19:45:21 kaa systemd[1]: Stopped target Sleep.

odinho - Velmont

Posted 2011-11-14T13:43:54.920

Reputation: 141

1that's the only one that worked for me, too (using Linux Mint 19 Cinnamon) – Suzana – 2020-01-16T14:26:39.910

1

modified better verision of steps answer

grep ': Awake' /var/log/pm-suspend.log

edit haha thanks for the comments :D

Jacek Pietal

Posted 2011-11-14T13:43:54.920

Reputation: 111

And you won a useless use of cat point! – gniourf_gniourf – 2014-06-26T15:59:53.887

No upvotes for useless use of 'cat'. – Magellan – 2014-06-26T16:01:38.633

0

You can use tuptime for track the system startup/shutdown life.

Rfraile

Posted 2011-11-14T13:43:54.920

Reputation: 101

Does not provide the information the OP requested. – wieczorek1990 – 2016-06-24T08:39:58.000

0

I think this is a very solid way to do it:

systemd[1]: Started Run anacron jobs at resume

Search for when the OS starts anacron, will happen however the machine is turned on

Jay Day Zee

Posted 2011-11-14T13:43:54.920

Reputation: 101

0

What are you using to initiate the standby?

If you can use a script, then after the line

echo -n "standby" > /proc/acpi/sleep

you could have the line

echo `date +%s` >> /var/log/wakeups.log

Or something similar. This would mean that the first thing the machine did when it woke up was to write the current time and date to a log file (n seconds since epoch).

Then tail -1 /var/log/wakeups.log would give you the last time. You could could subtract this from the current time to get seconds since the last wakeup.

Paul

Posted 2011-11-14T13:43:54.920

Reputation: 52 173

0

Search for the last occurence of the string "PM: restore of devices complete" in /var/log/messages. If your machine has been up too long, then the log may be rotated, though.

teika kazura

Posted 2011-11-14T13:43:54.920

Reputation: 540

0

Extending Steps answer:

grep Awake /var/log/pm-suspend.log | tail -1

This will get the line with the last wakeup time.

thethakuri

Posted 2011-11-14T13:43:54.920

Reputation: 103

-1

on fedora using ripgrep

rg Suspend /var/log/messages

result:

34338:Jul 26 03:03:46 <hostname> systemd-sleep: Suspending system...

illucent

Posted 2011-11-14T13:43:54.920

Reputation: 21