小编Ser*_*gio的帖子

使用 SYSTEMCTL 状态的 grep 搜索结果到 IF CONDITION

首先,感谢您对此问题的调查!

我是 bash 脚本的新手,我想要实现的是:我目前必须经常在工作中重新启动一些服务,我正在尝试创建一个脚本来自动化它,我设法创建了所有内容,但我也想创建一个条件,其中如果 systemctl status = inactive 则 echo“好的,很好,一切都很好”,所以让我们继续,但如果没有,则 echo“将在 10 秒内重新尝试重新启动”;我使用 grep 从服务的 systemctl 状态中查找文本“active: active (running)”或“active: inactive (dead)”(由下面的 bluetooth.service 表示):

#!/bin/bash
#Restarting  bluetooth.service

clear
active_stop="Active: stopped (dead)"    
echo  "This script will RESTART the following service: Bluetooth.Service"
echo -e  "State the REASON for the RESTART: " >> /home/sjadmin/SYS_RESTART/ARCHIVE/sysrestart.log
read -p  "Press ENTER to continue or CTRL + C to cancel"

sudo systemctl stop bluetooth.service |grep active IF [[grep -q=$active_stop]] 

then read -p  "Service STOPPED, please confirm status bellow, …
Run Code Online (Sandbox Code Playgroud)

grep if-statement conditional-statements systemctl

0
推荐指数
1
解决办法
5543
查看次数