4

I was wondering if it was possible to have a logging program document a random power failure. Clearly it won't have any time to log when the power fails, but is there a way to recognize a power failure occurred when it reboots after the failure? I know Red Hat (and Linux in general) has some pretty good logs (/var/log) but I don't think it has any native logging program that could handle a power failure.

If there is no logging program I was also debating changing the boot scripts. The BIOS is set for it to always be powered on; so if a power failure occurs, it automatically reboots.

The only time the system will be off is if a power failure happens; it will never be manually shut down.

Therefore, I was thinking of adding something (simple as a mail command) to the boot scripts because every boot after the very first boot marks a power failure. Unfortunately, I don't know which file or location or the actual syntax of such a command would be.

2 Answers 2

5

Is there a way to recognize a power failure occurred when it reboots after the failure?

See the SMBIOS's DMI "Wake-up Type".

$ sudo dmidecode | fgrep 'Wake-up Type'
    Wake-up Type: AC Power Restored

You can retrieve this programmatically via /sys/fireware/dmi/entries/1-0/…

1

Reboots are documented in the system journal on systems using systemd (e.g. RHEL 7, Fedora). You can look them up via:

# journalctl --list-boots
[..]
0 51545e9739db4eb1ab602dd338a937b1 Wed 2014-08-06 10:58:04 CEST—Mon 2014-08-18 22:31:43 CEST

Traditionally, reboots are also documented via wtmp, i.e. you can display them with the last command:

# last
[..]
reboot   system boot  3.11.10-301.fc20 Wed Jun  4 08:15 - 18:43  (10:27)

(Output from a Fedora 20 system)

The Ubuntu man page to last explains this mechanism:

   The pseudo user reboot logs in each time the system is rebooted.   Thus
   last  reboot will show a log of all reboots since the log file was cre‐
   ated.

(Depending on your distribution the wtmp file might be rotated during a long uptime such that the last reboot is not displayed)

To get notifications of reboots you can set them up indirectly via a cron job, e.g.:

#!/bin/bash
set -e
set -u
uptime=$(awk '{print $1}' /proc/uptime)
if [ -f /var/tmp/last_uptime ]; then
  last_uptime=$(cat /var/tmp/last_uptime)
  if [ $uptime -lt $last_uptime ]; then
    echo System was rebooted
    exit 1
  fi
fi
echo $uptime > /var/tmp/last_uptime

Alternatively, you can run a mail command as part of the boot process. With systemd it can be done like this:

# echo 'echo Just rebooted at $(date) | mail juser@example' >> /etc/rc.d/rc.local
# chmod 755 /etc/rc.d/rc.local

Other distributions that use something different than systemd might execute /etc/rc.local at the end of the boot sequence.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .