将 SYSTEMCTL 状态的 grep 搜索结果用于 IF 条件
Using the grep search result of a SYSTEMCTL status into a IF CONDITION
首先,感谢您的关注!
我是 bash 的新手,编写我想要实现的目标是:
我目前必须经常在我的工作中重新启动一些服务,我正在尝试创建一个脚本来自动化它,我设法创建了所有内容,但我还想创建一个条件,如果 systemctl status = inactive then echo "ok, fine, all good" 让我们继续,但如果没有,则 echo "re-start will be re-atempted in 10secs";我正在使用 grep 从服务的 systemctl 状态中查找文本 "active: active (running) or "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, press ENTER to continue..."
else read -p "Action failed";
fi
sudo systemctl status bluetooth.service |grep status1="$(Active: active (running))" >> /home/sjadmin/SYS_RESTART/ARCHIVE/sysrestart.log
sudo systemctl status bluetooth.service |grep active
echo "Service will be RESTARTED in 5 MINUTES, PLEASE DO NOT DISCONECT FROM THE SYSTEM..."
sleep 10s
sudo systemctl start bluetooth.service
read -p "Service RE-STARTED, please confirm status bellow, press ENTER to continue..."
sudo systemctl status bluetooth.service |grep active >> /home/sjadmin/SYS_RESTART/ARCHIVE/sysrestart.log
sudo systemctl status bluetooth.service |grep active
echo "System RESTARTED CORRECTLY, please find the log at the SYS_RESTART/ARCHIVE folder"
在此先感谢大家的帮助和支持! :)
我建议使用 systemctl show <service-name> --no-page
而不是解析状态输出:
status="$(systemctl show bluetooth.service --no-page)"
status_text=$(echo "${status}" | grep 'StatusText=' | cut -f2 -d=)
if [ "${status_text}" == "Running" ]
then
echo "It's running"
else
echo "Not running"
fi
active_state=$(echo "${status}" | grep 'ActiveState=' | cut -f2 -d=)
if [ "${active_state}" == "active" ]
then
echo "It's active"
else
echo "Not active"
fi
systemctl 处于活动状态 <服务>
root@freeswitch:~# systemctl is-active dialer
inactive
首先,感谢您的关注!
我是 bash 的新手,编写我想要实现的目标是: 我目前必须经常在我的工作中重新启动一些服务,我正在尝试创建一个脚本来自动化它,我设法创建了所有内容,但我还想创建一个条件,如果 systemctl status = inactive then echo "ok, fine, all good" 让我们继续,但如果没有,则 echo "re-start will be re-atempted in 10secs";我正在使用 grep 从服务的 systemctl 状态中查找文本 "active: active (running) or "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, press ENTER to continue..."
else read -p "Action failed";
fi
sudo systemctl status bluetooth.service |grep status1="$(Active: active (running))" >> /home/sjadmin/SYS_RESTART/ARCHIVE/sysrestart.log
sudo systemctl status bluetooth.service |grep active
echo "Service will be RESTARTED in 5 MINUTES, PLEASE DO NOT DISCONECT FROM THE SYSTEM..."
sleep 10s
sudo systemctl start bluetooth.service
read -p "Service RE-STARTED, please confirm status bellow, press ENTER to continue..."
sudo systemctl status bluetooth.service |grep active >> /home/sjadmin/SYS_RESTART/ARCHIVE/sysrestart.log
sudo systemctl status bluetooth.service |grep active
echo "System RESTARTED CORRECTLY, please find the log at the SYS_RESTART/ARCHIVE folder"
在此先感谢大家的帮助和支持! :)
我建议使用 systemctl show <service-name> --no-page
而不是解析状态输出:
status="$(systemctl show bluetooth.service --no-page)"
status_text=$(echo "${status}" | grep 'StatusText=' | cut -f2 -d=)
if [ "${status_text}" == "Running" ]
then
echo "It's running"
else
echo "Not running"
fi
active_state=$(echo "${status}" | grep 'ActiveState=' | cut -f2 -d=)
if [ "${active_state}" == "active" ]
then
echo "It's active"
else
echo "Not active"
fi
systemctl 处于活动状态 <服务>
root@freeswitch:~# systemctl is-active dialer
inactive