![Linux 优雅关机](https://linux22.com/image/566337/Linux%20%E4%BC%98%E9%9B%85%E5%85%B3%E6%9C%BA.png)
我听说“shutdown -h now”不是一个正常关机命令,因为它对所有进程执行类似于“kill -9”的操作,而不是“kill -15”。
这是真的吗?如果是的话,有什么更优雅的关闭方式可以确保一切都有时间自行清理?
Ps 主要使用 CentOS、Ubuntu 和 Debian。
答案1
shutdown -h now
将调用/etc/rc.d/rc
或/etc/init.d/rc
。该rc
脚本将调用新运行级别的 kill 脚本(对于 为 0 -h
,对于 为 6 -r
),然后调用任何启动脚本。
您将看到S30killprocs
或S00killall
或类似的东西,具体取决于您的发行版。这是在调用所有终止脚本以依次尝试正常停止每个服务之后发生的。它将kill -15
首先尝试,然后尝试kill -9
。
简短的回答:shutdown -h now
或者shutdown -r now
是优雅的。 halt
并且曾经是不优雅的,但是除非您使用该选项,否则reboot
它们只会为您呼叫。shutdown
-f
答案2
不,事实并非如此。shutdown
更改 init 级别,然后运行所有关闭脚本。这些脚本的作用取决于脚本。但它们通常不会终止进程,而是向它们发送结束信号。
这是手册摘录shutdown
:
shutdown brings the system down in a secure way. All logged-in users
are notified that the system is going down, and login(1) is blocked.
It is possible to shut the system down immediately or after a specified
delay. All processes are first notified that the system is going down
by the signal SIGTERM. This gives programs like vi(1) the time to save
the file being edited, mail and news processing programs a chance to
exit cleanly, etc. shutdown does its job by signalling the init
process, asking it to change the runlevel. Runlevel 0 is used to halt
the system, runlevel 6 is used to reboot the system, and runlevel 1 is
used to put to system into a state where administrative tasks can be
performed; this is the default if neither the -h or -r flag is given to
shutdown. To see which actions are taken on halt or reboot see the
appropriate entries for these runlevels in the file /etc/inittab.