Linux的优雅关机

我听说“shutdown -h now”不是一个优雅的closures命令,因为它为所有进程执行类似“kill -9”的操作,而不是“kill -15”。

这是真的吗?如果是这样的话,还有什么更优美的closures方式,以确保一切都有时间来清理自己?

Ps主要使用CentOS,Ubuntu和Debian。

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

你会看到S30killprocsS00killall或类似的东西取决于你的发行版。 这是在所有kill脚本被调用以试图按顺序停止每个服务之后。 它会尝试kill -15 ,然后kill -9

简短的回答: shutdown -h nowshutdown -r now是优雅的。 haltreboot过去是非优雅的,但他们只会打电话给你,除非你使用-f选项。

不,那不是真的。 shutdown更改init级别,然后运行所有closures脚本。 这些脚本的function取决于脚本。 但他们通常不会终止进程,而是将信号发送给他们。

所以这是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.