如何区分RHEL7上的崩溃和重启?

有没有办法确定RHEL7服务器是否通过systemctl(或reboot / shutdown别名)重新启动,或者服务器是否崩溃? 预系统这是相当容易确定的last -x runlevel ,但与RHEL7它不是很清楚。

    有不止一种方法来做到这一点,但我会覆盖我能想到的4个最好的方法。 (编辑:我在redhat.com上发布了一个清理版的公共文章,请参阅: 如何在RHEL 7中区分崩溃和正常重启 。

    (1)审计日志

    审计是惊人的。 您可以通过检查ausearch -m来查看它logging的所有不同事件。 针对手头的问题,它会logging系统关机和系统启动,因此您可以使用命令ausearch -i -m system_boot,system_shutdown | tail -4 ausearch -i -m system_boot,system_shutdown | tail -4 。 如果这报告一个SYSTEM_SHUTDOWN后跟一个SYSTEM_BOOT ,一切都很好; 但是,如果它连续报告2个SYSTEM_BOOT行,那么显然系统没有正常closures,如下例所示:

     [root@a72 ~]# ausearch -i -m system_boot,system_shutdown | tail -4 ---- type=SYSTEM_BOOT msg=audit(09/20/2016 01:10:32.392:7) : pid=657 uid=root auid=unset ses=unset subj=system_u:system_r:init_t:s0 msg=' comm=systemd-update-utmp exe=/usr/lib/systemd/systemd-update-utmp hostname=? addr=? terminal=? res=success' ---- type=SYSTEM_BOOT msg=audit(09/20/2016 01:11:41.134:7) : pid=656 uid=root auid=unset ses=unset subj=system_u:system_r:init_t:s0 msg=' comm=systemd-update-utmp exe=/usr/lib/systemd/systemd-update-utmp hostname=? addr=? terminal=? res=success' 

    (2)last -x

    和上面一样,但用简单的last -n2 -x shutdown reboot命令。 系统崩溃的示例:

     [root@a72 ~]# last -n2 -x shutdown reboot reboot system boot 3.10.0-327.el7.x Tue Sep 20 01:11 - 01:20 (00:08) reboot system boot 3.10.0-327.el7.x Tue Sep 20 01:10 - 01:20 (00:09) 

    或者系统有一个正常的重启:

     [root@a72 ~]# last -n2 -x shutdown reboot reboot system boot 3.10.0-327.el7.x Tue Sep 20 01:21 - 01:21 (00:00) shutdown system down 3.10.0-327.el7.x Tue Sep 20 01:21 - 01:21 (00:00) 

    (3)创build你自己的服务单位

    这是恕我直言,最好的办法,因为你可以定制它到任何你想要的。 有一百万种方法来做到这一点。 这是我刚刚编造的一个。 这下一个服务只在关机时运行。

     [root@a72 ~]# cat /etc/systemd/system/set_gracefulshutdown.service [Unit] Description=Set flag for graceful shutdown DefaultDependencies=no RefuseManualStart=true Before=shutdown.target [Service] Type=oneshot ExecStart=/bin/touch /root/graceful_shutdown [Install] WantedBy=shutdown.target [root@a72 ~]# systemctl enable set_gracefulshutdown.service Created symlink from /etc/systemd/system/shutdown.target.wants/set_gracefulshutdown.service to /etc/systemd/system/set_gracefulshutdown.service. 

    然后,当系统启动时,只有当上述关机服务创build的文件存在时,下一个服务才会启动。

     [root@a72 ~]# cat /etc/systemd/system/check_graceful.service [Unit] Description=Check if system booted after a graceful shutdown ConditionPathExists=/root/graceful_shutdown RefuseManualStart=true RefuseManualStop=true [Service] Type=oneshot RemainAfterExit=true ExecStart=/bin/rm /root/graceful_shutdown [Install] WantedBy=multi-user.target [root@a72 ~]# systemctl enable check_graceful Created symlink from /etc/systemd/system/multi-user.target.wants/check_graceful.service to /etc/systemd/system/check_graceful.service. 

    因此,在任何给定的时间,我可以通过systemctl is-active check_graceful来检查以前的启动是否在正常关机后完成,例如:

     [root@a72 ~]# systemctl is-active check_graceful && echo YAY || echo OH NOES active YAY [root@a72 ~]# systemctl status check_graceful ● check_graceful.service - Check if system booted after a graceful shutdown Loaded: loaded (/etc/systemd/system/check_graceful.service; enabled; vendor preset: disabled) Active: active (exited) since Tue 2016-09-20 01:10:32 EDT; 20s ago Process: 669 ExecStart=/bin/rm /root/graceful_shutdown (code=exited, status=0/SUCCESS) Main PID: 669 (code=exited, status=0/SUCCESS) CGroup: /system.slice/check_graceful.service Sep 20 01:10:32 a72.example.com systemd[1]: Starting Check if system booted after a graceful shutdown... Sep 20 01:10:32 a72.example.com systemd[1]: Started Check if system booted after a graceful shutdown. 

    或者在不正常的关机之后:

     [root@a72 ~]# systemctl is-active check_graceful && echo YAY || echo OH NOES inactive OH NOES [root@a72 ~]# systemctl status check_graceful ● check_graceful.service - Check if system booted after a graceful shutdown Loaded: loaded (/etc/systemd/system/check_graceful.service; enabled; vendor preset: disabled) Active: inactive (dead) Condition: start condition failed at Tue 2016-09-20 01:11:41 EDT; 16s ago ConditionPathExists=/root/graceful_shutdown was not met Sep 20 01:11:41 a72.example.com systemd[1]: Started Check if system booted after a graceful shutdown. 

    (4)journalctl

    值得一提的是,如果将systemd-journaldconfiguration为保留持久日志,则可以使用journalctl -b -1 -n查看以前引导的最后几行(默认为10行)(- -b -2是之前的开机等)。 系统正常重启的示例:

     [root@a72 ~]# mkdir /var/log/journal [root@a72 ~]# systemctl -s SIGUSR1 kill systemd-journald [root@a72 ~]# reboot ... [root@a72 ~]# journalctl -b -1 -n -- Logs begin at Tue 2016-09-20 01:01:15 EDT, end at Tue 2016-09-20 01:21:33 EDT. -- Sep 20 01:21:19 a72.example.com systemd[1]: Stopped Create Static Device Nodes in /dev. Sep 20 01:21:19 a72.example.com systemd[1]: Stopping Create Static Device Nodes in /dev... Sep 20 01:21:19 a72.example.com systemd[1]: Reached target Shutdown. Sep 20 01:21:19 a72.example.com systemd[1]: Starting Shutdown. Sep 20 01:21:19 a72.example.com systemd[1]: Reached target Final Step. Sep 20 01:21:19 a72.example.com systemd[1]: Starting Final Step. Sep 20 01:21:19 a72.example.com systemd[1]: Starting Reboot... Sep 20 01:21:19 a72.example.com systemd[1]: Shutting down. Sep 20 01:21:19 a72.example.com systemd-shutdown[1]: Sending SIGTERM to remaining processes... Sep 20 01:21:19 a72.example.com systemd-journal[483]: Journal stopped 

    如果你得到这样的好输出,那么显然系统已经正常关机了。 也就是说,当我遇到不好的事情(系统崩溃)时,这并不是超可靠的。 有时索引变得怪异。

    有趣的是,昨天晚上我碰巧重启了一个CentOS 7系统,所以我有一个很好的日志来看看。

    在发生崩溃的情况下,崩溃时间和系统重新启动之间显然没有logging。

    在重新启动的情况下,这是非常明显的,因为你得到(几乎)systemd正在做的closures系统的日志。

    除了closures或进入单用户模式以外,在任何情况下您都不会看到这样一个日志条目:

     Jul 13 01:27:55 yaungol systemd: Stopped target Multi-User System. 

    您可以重新启动自己的系统,查看实际logging的内容。

    我不是特别喜欢答案,但这是我们从RH那里得到的答案。 我在这里张贴,以防别人帮忙。

    一种可能的方法是grep for /var/log/messages rsyslogd 。 优雅的关机将exiting on signal 15 。 崩溃不会。

    tac /var/log/messages | grep 'rsyslogd.*start\|rsyslogd.*exit'

    连续两个start行可能表示崩溃。 然后start然后exit可能表示重新启动。

    不幸的是,如果rsyslogd出现故障或在重新引导/崩溃之外重新启动,也可能会导致不好的结果。

    这似乎一贯地为“正常关机”( shutdownrebootsystemctl )以及“崩溃”(关机,重置, echo c > /proc/sysrq-trigger )工作:

    last -x | grep 'reboot\|shutdown'

    reboot行后跟shutdown行表示“正常关机”。 两个reboot行表示“崩溃”。