How to find the uptime since last wake from standby

17,004

Solution 1

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).

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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. :-)

Share:
17,004

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • gniourf_gniourf
    gniourf_gniourf about 10 years
    And you won a useless use of cat point!
  • Magellan
    Magellan about 10 years
    No upvotes for useless use of 'cat'.
  • cprn
    cprn over 8 years
    What if pm-suspend.log is empty? :(
  • cprn
    cprn over 8 years
    What if there's no command pmset found and no such file as pmset and pm-suspend.log is empty? :(
  • dayuloli
    dayuloli about 8 years
    pm-suspend.log was missing and this works for me (on my iMac)
  • raphinesse
    raphinesse about 8 years
    This worked for me on Ubuntu 16.04
  • wieczorek1990
    wieczorek1990 about 8 years
    Does not provide the information the OP requested.
  • Shiplu Mokaddim
    Shiplu Mokaddim almost 8 years
    Good to know that we can check the journal.
  • raphinesse
    raphinesse over 7 years
    Use journalctl -b 0 -o short-iso MESSAGE="PM: Finishing wakeup." | tail -1 | cut -d" " -f1 for just the time of the last wakeup
  • webbertiger
    webbertiger over 7 years
    If 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
  • Ramon Suarez
    Ramon Suarez over 6 years
    No such file in my computer (running Ubuntu 16.04 LTS)
  • dmatej
    dmatej over 6 years
    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.
  • plaisthos
    plaisthos over 6 years
    This is for Mac only
  • jeff_drumgod
    jeff_drumgod over 4 years
    show perfectly in MacOs Catalina
  • Suzana
    Suzana over 4 years
    that's the only one that worked for me, too (using Linux Mint 19 Cinnamon)
  • odinho - Velmont
    odinho - Velmont over 4 years
    @Suzana came with a tip for calculating the time since: $ datediff -f%H:%M:%S $(journalctl -n4 -u sleep.target -o short-iso | tail -n 1 | cut -d' ' -f 1) now -> 5:38:48 (fresse.org/dateutils)
  • odinho - Velmont
    odinho - Velmont over 4 years
    kinda useless use of tail and cut there, but ohwell ;)
  • Suzana
    Suzana over 4 years
    So how would you get the timestamp without tail and cut?
  • odinho - Velmont
    odinho - Velmont over 4 years
    @Suzana cut might be required here, but you can just use -n1 to remove the tail, maybe also add -q so journal doesn't add warning messages messing up. It's also possible to use JSON and jq instead of cut: journalctl -q -n1 -u sleep.target -o json | jq -r ._SOURCE_REALTIME_TIMESTAMP
  • Suzana
    Suzana over 4 years
    journalctl -n1 works only if you also add -r to get the newest timestamp but you are right, that's shorter.
  • IanB
    IanB over 3 years
    On Fedora, this worked for me: journalctl -b 0 | grep "System resumed" | tail -1
  • Hrishikesh Kadam
    Hrishikesh Kadam over 2 years
    I had to add -a to grep because I was getting grep: (standard input): binary file matches and some latest records were not shown. So the command which worked for me was - cat /var/log/syslog | grep -a 'systemd-sleep' | grep "Suspending\|resumed"
  • Admin
    Admin about 2 years
    In Ubuntu 22.04, I had to update to cat /var/log/syslog | grep -a 'systemd-sleep' | grep "Entering sleep state\|System returned from sleep"
  • Admin
    Admin about 2 years
    Yes, this is super useful. Sometimes the computer would have earlier logs renamed to syslog.1 and so on.