linux 在bash中使用条件循环变量(按特定顺序停止服务)

vtwuwzda  于 2023-01-16  发布在  Linux
关注(0)|答案(2)|浏览(128)

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

  • 根据服务名称上的特定词语查找特定服务
  • 按特定顺序停止这些服务**,例如:服务1、服务2等**
  • 检查这些服务是否已停止,如果有一个仍在运行,它将打印出其名称

这是我的进步,首先,我已经找到了基于一些词的服务,只存在与他们的名字如下,

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_1" ] //if service exists
then
      systemctl stop $service_1
else
      :
fi

if [ ! -z "$service_2" ] //if service exists
then
      systemctl stop $service_2
else
      :
fi

if [ ! -z "$service_3" ] //if service exists
then
      systemctl stop $service_3
else
      :
fi

问题来了,如你所知,停止服务时,完全停止可能需要一段时间,因此根据上述条件,它将继续停止第二个服务,而不完全停止前一个服务。我希望按顺序停止服务,因此在停止第二个服务之前,应完全停止第一个服务,以此类推。如何做到这一点?我认为这可以通过循环来实现,但我错过了方法。关于第三个用例,我定义了以下变量,在过滤服务的状态输出后,将单词“inactive”作为服务停止的指示符:

service_1_status=$(systemctl status $service_1 | grep -e "inactive")
service_2_status=$(systemctl status $service_2 | grep -e "inactive")
service_3_status=$(systemctl status $service_3 | grep -e "inactive")

然后,我尝试循环它们,以找出是否有一个服务仍然是活动的,以打印其名称如下:

arr=($service_1_status $service_2_status $service_3_status $service_q_inf_status)
for i in "${arr[@]}"
do
      if "${arr[$i}" != "inactive"
      then
            echo $i
      else
            echo "All Services are now Stopped"
      fi
done

我不知道这个语法是否完全可操作,因为它不起作用,但这是我想做的逻辑,正确的方法是什么?

u3r8eeie

u3r8eeie1#

# Service variables.

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}')

# Service stopping code.

[[ -n "${service_1}" ]] && systemctl stop "{$service_1}"
[[ -n "${service_2}" ]] && systemctl stop "{$service_2}"
[[ -n "${service_3}" ]] && systemctl stop "{$service_3}"

service_1_status=$(systemctl status $service_1 | grep -e "inactive")
service_2_status=$(systemctl status $service_2 | grep -e "inactive")
service_3_status=$(systemctl status $service_3 | grep -e "inactive")

[[ -n "${service_1_status}" ]] && \
[[ -n "${service_2_status}" ]] && \
[[ -n "${service_3_status}" ]] && \
echo "All services stopped."

这是一个3输入测试。只有当所有三个变量都等于无效时才能通过。

dffbzjpn

dffbzjpn2#

#!/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_1" ] # if service exists
then
      systemctl start $service_1
      sleep 30 # 30s
      echo "$service_1 started"
else
      :
fi

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

if [ ! -z "$service_2" ] # if service exists
then
      if [ $(systemctl is-active $service_2) == "active" ] # if service is active
      then
            if [ ! -z "$service_3" ] # if service exists
            then
                  systemctl start $service_3
                  sleep 30 # 30s
                  echo "$service_3 started"
            else
                  :
            fi
      else
      echo "$service_2 is either inactive or failed, Please check !"
      exit
      fi
elif [ ! -z "$service_3" ]
then
      systemctl start $service_3
      sleep 30 # 30s
      echo "$service_3 started"
else
:
fi`enter code here`

相关问题