首先,感谢您就这个!
我在bash的脚本非常新手什么,我想实现的是:目前我有我的工作经常重新启动一些服务,我想创建一个纸条给automatise它,我设法创造的一切,但我想还创建有条件的地方,如果systemctl状态=无效然后回声“OK,很好,都好”所以让我们继续进行,但如果没有,那么回声“重新启动将重新atempted在10secs”;我使用grep来查找文本“活动:活动(运行)或”活动:从服务上的systemctl状态无效(死)(由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