Linux正常关机


17

我听说“ shutdown -h now”不是正常的关闭命令,因为它对所有进程都执行类似于“ kill -9”的操作,而不是“ kill -15”。

这是真的吗?如果是的话,那么一种更优雅的关闭方式来确保所有内容在其自身拥有清理后,又是什么?

Ps主要使用CentOS,Ubuntu和Debian。


我将等待其他人的鸣叫,但这听起来根本不对。-h只是告诉它将系统置于HALT模式,而不是重新引导。而“现在”仅意味着没有关闭用户的宽限期。不确定其中任何一种将如何修改关机的实际终止行为
Jeffery Smith,

4
请阅读手册页进行关机(man shutdown)。它完全解释了这一点。
Andrew Schulman

Answers:


15

shutdown -h now会致电/etc/rc.d/rc/etc/init.d/rc。该rc脚本将调用新运行级别的kill脚本(0代表-h,6代表-r),然后调用任何启动脚本。

您会看到S30killprocsS00killall类似的内容,具体取决于您的发行版。这是在调用所有终止脚本以尝试依次正常停止每个服务之后发生的。它将kill -15首先尝试,然后再尝试kill -9

简短的回答:shutdown -h now还是shutdown -r now很优雅。 haltreboot以前是不优雅,但他们将只叫shutdown了你,除非你使用的-f选项。


12

不,那不是真的。shutdown更改初始化级别,然后运行所有关闭脚本。这些脚本的作用取决于脚本。但是它们通常不终止进程,而是向它们发送结束信号。

这是以下内容的手册摘录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.

1
好吧,在我使用过的大多数(即使不是全部)发行版中,runlevel 6(重新引导)rc脚本所做的最后一件事是运行全局killall,以捕获任何挥之不去或挂起的进程。但这是在所有正常的停止脚本完成之后运行的。
适配器

2
@adaptr当然。但是过程有机会优雅地结束。如果他们没有,那就别无他法了。
mailq
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.