AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 789442
Accepted
kwb
kwb
Asked: 2016-07-13 12:35:18 +0800 CST2016-07-13 12:35:18 +0800 CST 2016-07-13 12:35:18 +0800 CST

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

  • 772

有没有办法确定 RHEL7 服务器是否通过 systemctl(或 reboot / shutdown 别名)重新启动,或者服务器是否崩溃?Pre-systemd 使用 很容易确定last -x runlevel,但使用 RHEL7 就不太清楚了。

server-crashes systemd rhel7 system-monitoring
  • 4 4 个回答
  • 8216 Views

4 个回答

  • Voted
  1. Michael Hampton
    2016-07-14T10:44:17+08:002016-07-14T10:44:17+08:00

    有趣的是,我昨晚碰巧重新启动了一个 CentOS 7 系统,所以我有一个很好的日志可以查看。

    在崩溃的情况下,显然在崩溃和系统重新启动之间没有任何记录。

    在重新启动的情况下,这很明显,因为您会获得(几乎)systemd 为关闭系统所做的所有事情的日志。

    除了关闭或进入单用户模式之外,您在任何情况下都不太可能看到的此类日志条目是:

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

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

    • 8
  2. Best Answer
    rsaw
    2016-09-20T21:43:11+08:002016-09-20T21:43:11+08:00

    有不止一种方法可以做到这一点,但我会介绍我能想到的 4 种最好的方法。(编辑:我在 redhat.com 上作为公开文章发布了此版本的清理版本。请参阅:如何区分 RHEL 7 中的崩溃和正常重启。)

    (1) 审核日志

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

    [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) 最后-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) 创建自己的服务单元

    恕我直言,这是最好的方法,因为您可以根据需要对其进行调整。有一百万种方法可以做到这一点。这是我刚编的一个。下一个服务仅在关机时运行。

    [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.
    

    那么当系统启动时,只有在上面的shutdown服务创建的文件存在的情况下,这个next服务才会启动。

    [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-journald为保留持久日志,则可以使用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
    

    如果你得到这样的良好输出,那么显然系统已正常关闭。也就是说,根据我的经验,当坏事发生(系统崩溃)时,它并不是超级可靠的。有时索引会变得很奇怪。

    • 6
  3. kwb
    2016-07-14T10:38:56+08:002016-07-14T10:38:56+08:00

    我不是特别喜欢这个答案,但这是我们从 RH 得到的答案。我把它贴在这里以防它帮助别人。

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

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

    两条连续start的线可能表示崩溃。astart后跟 anexit可能表示重新启动。

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

    • 5
  4. kwb
    2016-07-22T09:27:47+08:002016-07-22T09:27:47+08:00

    这似乎对“正常关机”(shutdown, reboot, systemctl)以及“崩溃”(关机、重置、echo c > /proc/sysrq-trigger)始终有效:

    last -x | grep 'reboot\|shutdown'

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

    • 1

相关问题

  • 内核错误的解释 - Eeeek!page_mapcount 变为负数

  • 为什么我的服务器意外宕机?

  • 如何追踪 Windows Server 2008 崩溃的原因?

  • apache在php中与exec崩溃

  • 对可怕的 0x9C BSOD 进行故障排除

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve