shell脚本检查服务状态

时间:2020-03-05 15:31:47  来源:igfitidea点击:

shell脚本检查服务是否正在运行,如果不是,则尝试启动它。

它对系统管理员有益,以确保关键的服务一直运行。

shell脚本检查服务状态

此脚本将检查任何init.d服务的状态,如果它未运行,则启动该服务。

#!/bin/bash
if [ "$#" = 0 ]
then
echo "Usage 
bobbin@theitroad:/$./service.sh apparmor
Service apparmor is not running
Found startap script /etc/init.d/apparmor. Start it? Y/n ? Y
Starting service...
* Starting AppArmor profiles [OK]
" exit 1 fi service= is_running=`ps aux | grep -v grep| grep -v "##代码##" | grep $service| wc -l | awk '{print }'` if [ $is_running != "0" ] ; then echo "Service $service is running" else echo initd=`ls /etc/init.d/| grep $service | wc -l | awk '{ print }'` if [ $initd = "1" ]; then startup=`ls /etc/init.d/| grep $service` echo -n "Found startap script /etc/init.d/${startup}. Start it? Y/n ? " read answer if [ $answer = "y" -o $answer = "Y" ]; then echo "Starting service..." /etc/init.d/${startup} start fi fi fi

结果

##代码##