1、关机
关机有几种方式:halt、shutdown、poweroff、init
①、halt
david@david-PC:~$ halt --help
halt [OPTIONS...]
Halt the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
单独执行halt,只关机,不关电源;
加上-p参数后,关机、关电源
②、shutdown
相比于其他指令,shutdown是一个安全的指令,只有root权限才能执行此指令。
执行此指令后,系统管理员会通知所有已登录用户系统即将关闭,且login指令将冻结,新的用户无法再登录了。
原理是:
执行此指令后,其实是发送信号【signal】给init指令,要求其改变runlevel运行级别为0,按这种方式实现关机。
runlevel 0 关机
runlevel 6 重启
david@david-PC:~$ shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]
Shut down the system.
--help Show this help
-H --halt Halt the machine
-P --poweroff Power-off the machine
-r --reboot Reboot the machine
-h Equivalent to --poweroff, overridden by --halt
-k Don't halt/power-off/reboot, just send warnings
--no-wall Don't send wall message before halt/power-off/reboot
-c Cancel a pending shutdown
shutdown -h now (-h等同于–poweroff),立即关机、关电源
③、poweroff
等同于 halt + 切断电源指令
该指令在关闭OS前还会发送ACPI指令,通知切断电源供应;如果系统不支持ACPI,该指令等同于halt(即只关闭系统,不断电)
david@david-PC:~$ poweroff --help
poweroff [OPTIONS...]
Power off the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
④、init
传递给init指令不同的runlevel数字信号来达到关机或重启目的。
runlevel 0 关机
runlevel 6 重启
david@david-PC:~$ init --help
init [OPTIONS...] {COMMAND}
Send control commands to the init daemon.
--help Show this help
--no-wall Don't send wall message before halt/power-off/reboot
Commands:
0 Power-off the machine
6 Reboot the machine
2, 3, 4, 5 Start runlevelX.target unit
1, s, S Enter rescue mode
q, Q Reload init daemon configuration
u, U Reexecute init daemon
2、重启
reboot
david@david-PC:~$ reboot --help
reboot [OPTIONS...] [ARG]
Reboot the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
总结
:
halt、poweroff、reboot 都不是安全的关机/重启指令。
因为他们在关闭系统时是删除所有的进程,而非优雅平稳的终止它们,如果还有其他用户正在工作,可能会造成数据丢失。
一般情况下,都建议使用shutdown配合-h或-r来实现关闭、重启系统。
几个例子:
shutdown -r now
立即重启
shutdown -r 10
10分钟后重启
shutdown -r 18:30
在18:30时重启,特殊情况下,如果在18:30之后执行该指令,则表示在第二天的18:30分重启。