I’m looking for a reliable way to log when my laptop is:

  • powered down
  • boots up
  • goes to sleep
  • wakes up

Currently I’m checking both the systemd-suspend and tlp systemctl services, but these don’t really feel very robust, and I don’t have TLP installed on all my machines.

Is there an easier way to do this, or a better systemctl unit that logs all the power states of my machine. Preferably laptop agnostic?

Laptop snippet so far:

journalctl --since -9days -u systemd-suspend -u tlp \
    | grep -P "Finish|Start|Stopped" | sed '/.*Finished TLP.*/d;
            s|Starting TLP.*|╭╴System Boot  |;
 s|Starting System Suspend.*|┤ · Sleep      |;
 s|Finished System Suspend.*|├ · Wake       |;
             s|Stopped TLP.*|╰╴Power Off    |;' \
    | sed -r 's|^(.*:[0-9]+)+:[0-9]+.*:(.*)|   \1 \2 |'
  • @just_another_person
    link
    21 day ago

    Are you asking about uptime monitoring on a remote host, or the actual last state of the machine once X happens via logged info on the machine?

    If the former, you can add a hook that pings somewhere before X action takes place.

    For the latter, it should be in dmesg, both going not and coming out of different states.

    • @[email protected]OP
      link
      fedilink
      11 day ago

      I guess uptime monitoring of any host, with specific timestamps for when the system is suspended/resumed.
      (Yes I understand that a server ideally shouldn’t sleep)

      • @[email protected]
        link
        fedilink
        English
        31 day ago

        in your shoes i would use a loop to iterate through journalctl’s up/down sessions getting the first and last lines of each and then use awk to printout the timestamps and sed to add in power state changes.