Centos7程序里面判断系统是否正在关机或者重启(Check if Centos7 is rebooting or shuting down in program.)

在Centos7系统中,由于废弃了runlevel命令,可以通过`systemctl list-units --type target`命令来判断系统状态。如果查询结果中同时存在`reboot.target`和`shutdown.target`,则表示系统正在重启;只有`shutdown.target`表示正在关机。这是由于Centos7采用了target方式管理服务,与Centos6.5的runlevel机制不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     程序在OS关机时需要做善后处理,那么就要判断系统是否是在reboot或者关机,Centos6.5可以通过runlevel命令查询到当前运行级别,重启时变为6,关机时变为0. Centos7废弃了runlevel的这一套,那么怎么判断呢?


1. 判断方法:
当 systemctl list-units --type target  命令同时查询到如下reboot和shutdown两个输出时,表示正在reboot
reboot.target       loaded inactive dead   start Reboot
shutdown.target     loaded inactive dead   start Shutdown

如果只能查到shutdown而没有reboot,则是在关机。

2. 原理分析
        Centos7采用target方式管理系统,Centos6.5那种按照runlevel来区分OS运行级别的方式已被废弃。新旧对应关系如下:
    from: 
    https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Targets.html#tabl-Managing_Services_with_systemd-Targets-Runlevels

     Table 9.6. Comparison of SysV Runlevels with systemd Targets
Runlevel Target Units Description
0 runlevel0.targetpoweroff.target Shut down and power off the system.
1 runlevel1.targetrescue.target Set up a rescue shell.
2 runlevel2.targetmulti-user.target Set up a non-graphical multi-user system.
3 runlevel3.targetmulti-user.target Set up a non-graphical multi-user system.
4 runlevel4.targetmulti-user.target Set up a non-graphical multi-user system.
5 runlevel5.targetgraphical.target Set up a graphical multi-user system.
6 runlevel6.targetreboot.target Shut down and reboot the system.

3. reboot时,查询命令完整输出。

 systemctl list-units --type target
UNIT                LOAD   ACTIVE   SUB    JOB   DESCRIPTION
basic.target        loaded active   active stop  Basic System
cryptsetup.target   loaded active   active stop  Encrypted Volumes
final.target        loaded inactive dead   start Final Step
local-fs-pre.target loaded active   active stop  Local File Systems (Pre)
local-fs.target     loaded active   active stop  Local File Systems
network.target      loaded active   active stop  Network
paths.target        loaded active   active stop  Paths
reboot.target       loaded inactive dead   start Reboot
shutdown.target     loaded inactive dead   start Shutdown
slices.target       loaded active   active stop  Slices
sockets.target      loaded active   active stop  Sockets
swap.target         loaded active   active stop  Swap
sysinit.target      loaded active   active stop  System Initialization
timers.target       loaded active   active stop  Timers
umount.target       loaded inactive dead   start Unmount All Filesystems

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
JOB    = Pending job for the unit.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值