开发者

Linux系统下的重启,关机命令以及reboot挂死问题解决方案

目录
  • linux开关机
    • reboot 重启
    • shutdown 关机
    • poweroff 用于关闭计算机
    • 问题描述
    • 问题解决尝试
    • 这几种命令区别在哪里呢
  • 总结

    Linux开关机

    在Linux下,我们常用 shutdown、reboot、poweroff、halt等进行关机或重启

    另外还有就是直接使用 init 修改循行等级进行,如:init 0、init 6等

    各命令的作用:

    reboot 重启

    可接参数如下:

    Reboot the system.
    
         --help      Show this help
         --halt      Halt the MAChine
      -p --poweroff  Switch off the machine   
         WlcFVUozp--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

    这里主要讲一下`reboot -f`,强制重启

    在使用`reboot`这个命令过程中,有在Centos8.2系统中遇到reboot挂死,约等待

    30mins才能关机然后重新启动,后来修改命令为`reboot -f`之后,问题解决

    关机流程:

    • 1.通知所有运行中的进程:系统将发送SIGTERM信号给所有正在运行的进程,通知它们系统即将关机。
    • 2.停止所有运行中的进程:系统会逐个终止所有正在运行的进程,直到没有剩余进程为止。
    • 3.关闭所有打开的文件:系统会关闭所有打开的文件,确保所有数据都被正确地写入到磁盘中。
    • 4.停止所有编程客栈设备:系统会停止所有设备的运行,包括磁盘、网络设备和其他硬件设备。
    • 5.关闭内核:系统会释放内核占用的所有资源,并退出内核运行状态。
    • 6.重启计算机:系统会发送一个重启指令给计算机,计算机将重新启动,并加载操作系统。

    相似的reboot命令:

    system reboot
    shutdown -r now
    init 6

    shutdown 关机

    [root@localhost ~]# 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

    poweroff 用于关闭计算机

    halt 用于关闭计算机或进入停机状态,有些系统是调用shupythontdown -h 或poweroff

    再深入了解一下系统运行等级的差别:

    首先明确一下linux/RedHat系的runlevel运行级别和Debian系的runlevel是有所不同的。

    redhat系的runlevel定义如下:

    • runlevel 0: halt 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
    • runlevel 1: single user 单用户工作状态,root权限,用于系统维护,禁止远程登陆
    • runlevel 2: multiuser without network 多用户状态(没有NFS)
    • runlevel 3: multiuser 完全的多用户状态(有NFS),登陆后进入控制台命令行模式
    • runlevel 4: unuse 系统未使用,保留
    • runlevel 5: x11 X编程11控制台,登陆后进入图形GUI模式
    • runlevel 6: reboot 系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

    在debian/Ubuntu中,runlevel的定义为:

    0 - Halt
    1 - Single
    2 - Full multi-user with display manager (GUI)
    3 - Full multi-user with display manager (GUI)
    4 - Full multi-user with display manager (GUI)
    5 - Full multi-user with display manager (GUI)
    6 - Reboot

    可以发现2~5级是没有任何区别的。

    Ubuntu系统默认的运行级别为2。

    因此,我们可以使用init 0来执行halt关机,使用init 6来执行reboot。

    其实shutdown命令也是在执行完一系列操作后,比如说逐个关闭进程/服务,调用sync将数据写入磁盘等,然后调用init0或init6来执行关机或重启而halt实际上是调用shutdown -h now,可以不理会系统当前状态而直接关机,但在有的系统中,halt不会关闭电源,而只关闭了os。

    问题描述

    reboot 发生挂死,reboot关机时卡了近半个小时,才能关机下去,重启上电流程无异常。

    环境:

    CentOS Stream release 8
    Linux 4.18.0-500.el8.x86_64
    systemd 239 (239-76.el8)

    问题解决尝试

    首先当然从系统入手,我查询了/var/log/messages上面记录的内容,发现没什么错误信息

    先没有计较太多,我直接使用yum update更新了一下我的系统,再次尝试

    1,百度寻医问药,修改/etc/systemd/system.conf ,修改系统参数,但是并未有改善,此方案不适用我这边的状况

    2,尝试其他的reboot方案,都会发生挂死,命令如下:

    system reboot
    shutdown -r now
    init 6

    3,最后尝试使用reboot -f,此时系统reboot 正常,并未发现重启挂死的状况

    20230914 解决了reboot关机卡死状况,解决办法如下:

    1、新建/etc/systemd/system/rc-local.service并写入

    	[Unit]
    	Description=/etc/rc.d/rc.local Compatibility
    	ConditionFileIsExecutable=/etc/rc.d/rc.local
    	After=network.target
    	[Service]
    	Type=forking
    	ExecSt编程art=/etc/rc.d/rc.local start
    	TimeoutSec=5
    	RemainAfterExit=yes

    2、备份/etc/systemd/system.conf

    cp -a /etc/systemd/system.conf /etc/systemd/system.conf_bak

    3、修改文件

    sed -i 's/#DefaultTimeoutStopSec=90s/DefaultTimeoutStopSec=30s/g' /etc/systemd/system.conf

    4、重新加载

    systemctl daemon-reload

    这几种命令区别在哪里呢

    目前仅仅知道 reboot -f 会 强制重新开机,不调用shutdown指令的功能,看起来跟普通的重启动作是不同的,或许会发生数据丢失的状况,从描述来看,少做了很多动作。

    reboot命令OS的动作:

    1、结束所有运行中的进程:系统会发送SIGTERM信号给所有正在运行的进程,通知它们系统即将重启。

    2、保存当前系统状态:系统会将当前系统的状态保存到/var/log/wtmp文件中,包括系统的运行时间、用户登录信息等。

    3、停止所有设备:系统会停止所有设备的运行,包括磁盘、网络设备和其他硬件设备。

    4、重启计算机:系统会发送一个重启指令给计算机,计算机将重新启动,并加载操作系统

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新运维

    运维排行榜