在bash中使用条件对变量进行循环(按特定顺序启动服务)



我正在bash中创建一个脚本来执行以下用例,

  • 根据名称上的特定单词查找特定服务
  • 按特定顺序启动这些服务,如:service_3、service_2等等
  • 在启动下一个服务之前,它将确保上一个服务已完全启动,如果未完全启动,它将等待(例如:4分钟(,直到完全启动;如果仍未完全启动,则它将尝试停止然后启动,它会打印出一条消息,表示服务需要调查并中断流程

这是我的进步,首先,我发现了基于一些单词的服务,这些单词的名称如下,

service_1=$(ls /etc/systemd/system | grep -e text1 | awk -F ' ' '{print $1}')
service_2=$(ls /etc/systemd/system | grep -e text2 | awk -F ' ' '{print $1}')
service_3=$(ls /etc/systemd/system | grep -e text3 | awk -F ' ' '{print $1}')

然后,我按照我想要的顺序停止了它们,检查第三个服务是否停止,然后再继续下面的第二个服务,

if [ ! -z "$service_3" ] //if service exists
then
systemctl start $service_3
else
:
fi
service_3_status=$(systemctl status $service_3 | grep -e "active")
if [ $service_3_status == "active"]
if [ ! -z "$service_2" ] //if service exists
then
systemctl start $service_2
else
:
fi
else
systemctl start $service_3
fi
service_2_status=$(systemctl status $service_2 | grep -e "active")
if [ $service_2_status == "active"]
if [ ! -z "$service_1" ] //if service exists
then
systemctl start $service_1
else
:
fi
else
systemctl start $service_2
fi

我想做的是在服务上循环,以确保第三个服务在移动到第二个服务之前完全启动,依此类推,如果有一个服务没有完全启动,它会等待直到完全启动(例如:4分钟(,但不会移动到下一个服务,如果它仍然停止,它会尝试停止和启动它,如果它仍然没有启动,它会打印出一条带有服务名称的消息,说明它无法启动,这需要调查。根据上面的代码,如果它没有完全启动,它将尝试启动服务,同时它将继续下一个不是我想要的行为,正确的方法是什么?

在对systemd的文档进行快速检查时,可以选择/处理等待/检查服务或是否有单元文件,但这里有一个嵌套循环,可以执行您想要的操作。

#!/usr/bin/env bash
##: Do your variable service assignment here
for service in "$service_3" "$service_2" "$service_1"; do
if [[ -n "$service" ]]; then
systemctl start "$service"
until systemctl status active "$service" >/dev/null; do
sleep 10
systemctl start "$service"
done
fi
done

  • 如果需要,在启动服务后添加sleep

请参阅how-can-a-systemd-service-flag-that-it-is-ready-so-that-other-services-can-wait获取有关如何以systemd方式进行操作的更多信息。

  • 如果文件存在,则延迟系统服务

  • 每次测试的服务运行脚本中的

  • systemctl-check-if-a-unit-service-or-target存在


虽然看起来像init.d。。。

#!/bin/bash

service_1=$(ls /etc/systemd/system | grep -e text1 | awk -F ' ' '{print $1}')
service_2=$(ls /etc/systemd/system | grep -e text2 | awk -F ' ' '{print $1}')
service_3=$(ls /etc/systemd/system | grep -e text3 | awk -F ' ' '{print $1}')

if [ ! -z "$service_3" ] # if service exists
then
systemctl stop $service_3
sleep 30 # 30s
echo "$service_3 stopped"
fi
if [ ! -z "$service_3" ] # if service exists
then
if [ $(systemctl is-active $service_3) == "inactive" ] # if service is not active
then
if [ ! -z "$service_2" ] # if service exists
then
systemctl stop $service_2
sleep 30 # 30s
echo "$service_2 stopped"
fi
else
echo "$service_3 is either active or failed, Please check !"
exit
fi
elif [ ! -z "$service_2" ]
then
systemctl stop $service_2
sleep 30 # 30s
echo "$service_2 stopped"
fi
if [ ! -z "$service_2" ] # if service exists
then
if [ $(systemctl is-active $service_2) == "inactive" ] # if service is not active
then
if [ ! -z "$service_1" ] # if service exists
then
systemctl stop $service_1
sleep 30 # 30s
echo "$service_1 stopped"
fi
else
echo "$service_2 is either active or failed, Please check !"
exit
fi
elif [ ! -z "$service_1" ]
then
systemctl stop $service_1
sleep 30 # 30s
echo "$service_1 stopped"
fi

最新更新