CentOS shell 检测服务状态

CentOS shell 检测服务状态,可以在停止的时候自动启动,用于服务器的进程守护

#!/bin/bash  
  
# 要检查的服务名称  
service_name="your_service_name"  
  
# 使用 systemctl is-active 命令检查服务状态  
if systemctl is-active "$service_name"  
then  
    echo "$service_name is running."  
else  
    echo "$service_name is not running."  
fi

如果只在没有允许时启动服务可以使用下面的代码

#!/bin/bash

# 定义你要检查的服务名称
service_name="your_service_name"

# 检查服务当前是否处于活动状态(即运行中)
if ! systemctl is-active --quiet "$service_name"; then
  # 如果服务未处于活动状态,则启动服务
  systemctl start "$service_name"
fi

原创文章,作者:admin,如若转载,请注明出处:https://ntib.cn/364.html

(0)
adminadmin
上一篇 2023年12月27日
下一篇 2024年1月23日

相关推荐

发表回复

登录后才能评论
WeChat