Linux关机&重启

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分重启。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值