46

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.

slhck
  • 223,558
  • 70
  • 607
  • 592
debian user
  • 461
  • 1
  • 4
  • 3

15 Answers15

31

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
  • 419
  • 4
  • 5
28

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
  • 381
  • 3
  • 4
  • 1
    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"` – Hrishikesh Kadam Dec 23 '21 at 12:05
  • 1
    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"` – Hrishikesh Kadam May 03 '22 at 15:28
  • 1
    Yes, this is super useful. Sometimes the computer would have earlier logs renamed to `syslog.1` and so on. – Yan King Yin May 19 '22 at 03:35
24

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
  • 391
  • 3
  • 4
23

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.
  • 3
    that's the only one that worked for me, too (using Linux Mint 19 Cinnamon) – Suzana Jan 16 '20 at 14:26
  • 2
    @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` (http://www.fresse.org/dateutils/) – odinho - Velmont Mar 12 '20 at 12:58
  • kinda useless use of tail and cut there, but ohwell ;) – odinho - Velmont Mar 12 '20 at 12:59
  • So how would you get the timestamp without tail and cut? – Suzana Mar 13 '20 at 16:12
  • 1
    @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` – odinho - Velmont Mar 14 '20 at 10:36
  • 1
    `journalctl -n1` works only if you also add `-r` to get the newest timestamp but you are right, that's shorter. – Suzana Mar 17 '20 at 14:55
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
  • 21
  • 2
1

modified better verision of steps answer

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

edit haha thanks for the comments :D

Jacek Pietal
  • 111
  • 4
0

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

rfmoz
  • 111
  • 3
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
  • 101
  • 1
0

If you want to know the last wake up time on Ubuntu 19.04 you can also:

grep sleep /var/log/auth.log*

auth.log.1:Feb 29 17:49:12 systemd-logind[694]: Operation 'sleep' finished.
auth.log.1:Mar  1 09:39:01 systemd-logind[694]: Operation 'sleep' finished.

I add this answer because I don't have the pm-suspend file like other answers suggested using.

0

Answer:

journalctl -r -S "1 day ago" | grep "System returned from sleep state" | head -1 | awk '{print $1, $2, $3}' | {read last_wake_up; echo $(($(date --date="now" +%s) - $(date --date="$last_wake_up" +%s)))} | {read sec ; eval "echo $(date -ud "@$sec" +'$((%s/3600/24)) days %H hours %M minutes %S seconds')"}

result:

0 days 07 hours 37 minutes 07 seconds

A bit shorter answer:

journalctl -r -S "1 day ago" | grep "System returned from sleep state" | head -1 | awk '{print $1, $2, $3}' | {read last_wake_up; echo $(( ($(date --date="now" +%s) - $(date --date="$last_wake_up" +%s) )/(60*60) ))"h"}

result:

6h

(tested on Ubuntu 22.04.1 LTS, kernel 5.15).


If you want to see minutes, change (60*60)))"h" to (60)))"m".

If you want to see days, change (60*60)))"h" to (60*60*24)))"d".

If you want to only see last wake up log, use:

journalctl -r -S "1 day ago" | grep "System returned from sleep state" | head -1

Please keep in mind that it's nice to have defined "since" -S in journalctl command, because there are a lot of logs there. On my machine I have 3 905 300 lines, and for the last day it's only 14 733.

"Since" can also be defined as a -S "2022-08-29 12:20".

user1660210
  • 139
  • 3
0

I used the kernel log to get the wakeup time.

wakeup=$(grep 'PM: suspend exit' /var/log/kern.log | tail -n1 | cut -c -15)
awake=$(( $(date +%s) - $(date --date "$wakeup" +%s) ))

This log ought to be pretty small and easy to search.

All the other answers either didn't work on my system (Ubuntu 22.04 LTS) or required installing something else.

Morphit
  • 575
  • 3
  • 9
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
  • 59,223
  • 18
  • 147
  • 168
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
  • 670
  • 1
  • 7
  • 11
0

Extending Steps answer:

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

This will get the line with the last wakeup time.

thethakuri
  • 103
  • 2
-1

on fedora using ripgrep

rg Suspend /var/log/messages

result:

34338:Jul 26 03:03:46 <hostname> systemd-sleep: Suspending system...
illucent
  • 31
  • 3