当前位置: 代码网 > 服务器>服务器>Linux > Linux系统下的重启,关机命令以及reboot挂死问题解决方案

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

2024年11月30日 Linux 我要评论
linux开关机在linux下,我们常用 shutdown、reboot、poweroff、halt等进行关机或重启另外还有就是直接使用 init 修改循行等级进行,如:init 0、init 6等各

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   
     --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 用于关闭计算机或进入停机状态,有些系统是调用shutdown -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 x11控制台,登陆后进入图形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
	execstart=/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、重启计算机:系统会发送一个重启指令给计算机,计算机将重新启动,并加载操作系统

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com