例如,我在/var/log/messages:
Mar 01 23:12:34 hostname shutdown: shutting down for system halt
Run Code Online (Sandbox Code Playgroud)
有没有办法找出导致关机的原因?例如,它是从控制台运行的,还是有人按下了电源按钮等?
Nic*_*nay 151
尝试以下命令:
显示上次重启条目的列表:
last reboot | less
显示最后关闭条目的列表:
last -x | less
或更准确地说:
last -x | grep shutdown | less
然而,你不会知道是谁做的。如果你想知道是谁做的,你需要添加一些代码,这意味着你下次会知道。
我在网上找到了这个资源。它可能对您有用:
nde*_*mou 66
last -x | head | tac
grep -iv ': starting\|kernel: .*: Power Button\|watching system buttons\|Stopped Cleaning Up\|Started Crash recovery kernel' \
/var/log/messages /var/log/syslog /var/log/apcupsd* \
| grep -iw 'recover[a-z]*\|power[a-z]*\|shut[a-z ]*down\|rsyslogd\|ups'
Run Code Online (Sandbox Code Playgroud)
运行此命令*并将输出与以下示例进行比较:
last -x | head | tac
Run Code Online (Sandbox Code Playgroud)
正常的关机和开机看起来像这样(请注意,您有一个关机事件,然后是一个系统启动事件):
runlevel (to lvl 0) 2.6.32- Sat Mar 17 08:48 - 08:51 (00:02)
shutdown system down ... <-- first the system shuts down
reboot system boot ... <-- afterwards the system boots
runlevel (to lvl 3)
Run Code Online (Sandbox Code Playgroud)
在某些情况下,您可能会看到这一点(请注意,没有关于关闭的行,但系统处于运行级别 0,即“暂停状态”):
runlevel (to lvl 0) ... <-- first the system shuts down (init level 0)
reboot system boot ... <-- afterwards the system boots
runlevel (to lvl 2) 2.6.24-... Fri Aug 10 15:58 - 15:32 (2+23:34)
Run Code Online (Sandbox Code Playgroud)
因断电而意外关机如下所示(请注意,您有一个系统启动事件,而没有先前的系统关机事件):
runlevel (to lvl 3) ... <-- the system was running since this momemnt
reboot system boot ... <-- then we've a boot WITHOUT a prior shutdown
runlevel (to lvl 3) 3.10.0-693.21.1. Sun Jun 17 15:40 - 09:51 (18:11)
Run Code Online (Sandbox Code Playgroud)
过滤最有趣的日志消息的 bash 命令是这样的:
grep -iv ': starting\|kernel: .*: Power Button\|watching system buttons\|Stopped Cleaning Up\|Started Crash recovery kernel' \
/var/log/messages /var/log/syslog /var/log/apcupsd* \
| grep -iw 'recover[a-z]*\|power[a-z]*\|shut[a-z ]*down\|rsyslogd\|ups'
Run Code Online (Sandbox Code Playgroud)
当发生意外断电或硬件故障时,文件系统将无法正确卸载,因此在下次启动时,您可能会收到如下日志:
EXT4-fs ... INFO: recovery required ...
Starting XFS recovery filesystem ...
systemd-fsck: ... recovering journal
systemd-journald: File /var/log/journal/.../system.journal corrupted or uncleanly shut down, renaming and replacing.
Run Code Online (Sandbox Code Playgroud)
当系统因用户按下电源按钮而关闭时,您会收到如下日志:
systemd-logind: Power key pressed.
systemd-logind: Powering Off...
systemd-logind: System is powering down.
Run Code Online (Sandbox Code Playgroud)
只有当系统有序关闭时,您才会得到这样的日志:
rsyslogd: ... exiting on signal 15
Run Code Online (Sandbox Code Playgroud)
当系统因过热而关闭时,您会收到如下日志:
critical temperature reached...,shutting down
Run Code Online (Sandbox Code Playgroud)
如果你有一个 UPS 并运行一个守护进程来监控电源和关机,你显然应该检查它的日志(NUT 日志在 /var/log/messages 但apcupsd 日志在 /var/log/apcupsd*)
笔记
*:这是last来自其手册页的描述:
last [...] prints information about connect times of users.
Records are printed from most recent to least recent.
[...]
The special users reboot and shutdown log in when the system reboots
or (surprise) shuts down.
Run Code Online (Sandbox Code Playgroud)
我们head用来保留最新的 10 个事件,我们tac用来反转排序,这样我们就不会被从最近事件到最近事件的最后一次打印的事实弄糊涂了。
for*_*sck 59
只有具有 root 权限的程序才能正常关闭系统。因此,当系统以正常方式关闭时,它要么是具有 root 权限的用户,要么是 acpi 脚本。在这两种情况下,您都可以通过检查日志来找到。acpi 关闭可能是由按下电源按钮、过热或电池电量不足(笔记本电脑)引起的。我忘记了第三个原因,当电源出现故障时 UPS 软件,它无论如何都会发出警报。
最近我有一个系统,它反复开始不正常地断电,结果发现它过热并且主板被配置为提前断电。系统没有机会保存日志,但幸运的是监控系统的温度显示它在断电前开始升高。
因此,如果是正常关闭,它将被记录,如果是入侵……祝您好运,如果是冷关闭,您最好的了解机会是控制和监视其环境。
小智 17
一些可能需要探索的日志文件:(找到了一个 Ubuntu 系统,但我希望它们存在于大多数 Linux/Unix 系统上)
/var/log/debug
/var/log/syslog (will be pretty full and may be harder to browse)
/var/log/user.log
/var/log/kern.log
/var/log/boot
Run Code Online (Sandbox Code Playgroud)
同样,这些日志文件存在于 Ubuntu 系统上,因此文件名可能不同。该tail命令是你的朋友。
我在 Debian 7.8 上也有类似的需求,并观察到日志中基本上没有明确明确的消息,这有点令人惊讶。
Grep through/var/log会告诉机器关闭的时间,显示正确的守护程序关闭等,但不是最初的原因。
shutdown[25861]: shutting down for system halt
Run Code Online (Sandbox Code Playgroud)
提到的其他解决方案 ( last -x) 没有多大帮助。
阅读/etc/acpi/powerbtn-acpi-support.sh内容包括:
如果 [ -x /etc/acpi/powerbtn.sh ] ; 然后
# 兼容来自 acpid 包的旧配置脚本
/etc/acpi/powerbtn.sh
elif [ -x /etc/acpi/powerbtn.sh.dpkg-bak ] ; 然后
# 兼容来自 acpid 包的旧配置脚本
# 仍然存在,因为它已被管理员更改
/etc/acpi/powerbtn.sh.dpkg-bak
别的
# 正常处理。
/sbin/shutdown -h -P 现在“按下电源按钮”
菲
请注意,显式文本作为shutdown命令的参数给出。我希望关闭程序会自动记录该字符串。
无论如何,为了获得明确的消息,我将下面的文本(作为 root)放在一个新创建的/etc/acpi/powerbtn.sh可执行文件中chmod a+x /etc/acpi/powerbtn.sh
#!/bin/sh
/etc/acpi/powerbtn.sh 中的记录器,大概是“按下电源按钮”
/sbin/shutdown -h -P 现在“按下电源按钮”
这样做可能比修改/etc/acpi/powerbtn-acpi-support.sh. 后一个选项可能会对 package 的下一次升级失去作用acpi-support-base。
请注意,Ubuntu 14.04 的处理方式有所不同(/etc/acpi/powerbtn.sh已经存在与acpid包不同的内容)。此外,Debian 8 可能会有所不同。随意提供变体。
现在,当按下电源按钮时,如下所示的一行出现在/var/log/messages,/var/log/syslog和 中/var/log/user.log:
logger: in /etc/acpi/powerbtn.sh, presumably Power button pressed
Run Code Online (Sandbox Code Playgroud)
现在这是日志中的显式消息。
简化使用last显示系统关闭条目和运行级别更改以及筛选shutdown和reboot:
last -x shutdown reboot
Run Code Online (Sandbox Code Playgroud)