Linux 优雅关机

Linux 优雅关机

我听说“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),然后调用任何启动脚本。

您将看到S30killprocsS00killall或类似的东西,具体取决于您的发行版。这是在调用所有终止脚本以依次尝试正常停止每个服务之后发生的。它将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.

相关内容