this post was submitted on 06 Feb 2025
44 points (97.8% liked)

Linux

49865 readers
714 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

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 |'
top 9 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 3 days ago (1 children)

I'm not clear on your use case here - the system obviously can't report if it's off. Initially I thought this was in the Home Assistant community, and I was going to suggest just pinging the machine at regular intervals from the HA system. That makes sense if you're trying to monitor various systems.

[–] [email protected] 4 points 3 days ago* (last edited 3 days ago) (1 children)

It can report just before it's shutting down. Hell, if I run shutdown -P 20:00 "OH WE GOIN DOWN" you bet your ass that I will get a wall message on every tty with that message at 8pm.

I'm just wondering how to reliably capture the shutdown messages without having to scan the entire system log. I just assumed that there would be one service file that I would have to check for these types of events, but apparently the best bet I have is the TLP service daemon which typically only runs on laptops.

[–] [email protected] 2 points 3 days ago (1 children)

I think last has it, too, but I'm not sure.

[–] [email protected] 1 points 2 days ago

Very good shout on last -- I see the boot up and power down states very nicely described there. Sadly, no hibernate info is there, so I would still need to check journalctl for that info

[–] [email protected] 4 points 4 days ago* (last edited 4 days ago) (1 children)

For boot and power down, create a service to log it.

For the others, an elogind script might be what you look for.

[–] [email protected] 3 points 4 days ago

Hmm! I never considered checking to see what loginctl was doing

And yep I think you're right, the most portable way of doing this is to maybe write my own service with hooks for OnBoot OnShutdown OnSleep OnWake (if that's a systemctl hook...)

[–] [email protected] 3 points 5 days ago (1 children)

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] 1 points 5 days ago (1 children)

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] 3 points 5 days 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.