In this tutorial, I will explain a shell script that check if service is running and if it is not then will try to start it.
Its good for system admin to make sure the crucial services are running all the time.
Shell Script to check service status
This script will check the status of any init.d service and start that service if it is not running.
#!/bin/bash if [ "$#" = 0 ] then echo "Usage $0 " exit 1 fi service=$1 is_running=`ps aux | grep -v grep| grep -v "$0" | grep $service| wc -l | awk '{print $1}'` if [ $is_running != "0" ] ; then echo "Service $service is running" else echo initd=`ls /etc/init.d/ | grep $service | wc -l | awk '{ print $1 }'` 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
Results
bobbin@linoxide:/$ ./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]
Learn this Script Line By Line
#check if service name passed to script as argument, if there no arguments (0) do next
if [ "$#" = 0 ]
then
"Usage $0 " #write to terminal usage
echo
#since no arguments – we need to exit script and user re-run it
exit 1
fi
#get service name from first argument
service=$1
#this check ,if service running using ps command, after we remove our process from the output, since script will also match, with wc we count number of matching lines.
is_running=`ps aux | grep -v grep| grep -v "$0" | grep $service| wc -l | awk '{print $1}'`
#is number of lines are not 0 do next
if [ $is_running != "0" ] ;
then
#just put this line to terminal
echo "Service $service is running"
#if number of precesses is 0
else
#Service $service is not running" #just put this string to terminal
echo
#checking for files in /etc/init.d
(directory with start-up scripts) with name similar to service
initd=`ls /etc/init.d/ | grep $service | wc -l | awk '{ print $1 }'`
#if there is script with similar name
if [ $initd = "1" ];
then
#this line get name of startup script (ls - lists files in directory
startup=`ls /etc/init.d/ | grep $service`
echo -n "Found startap script /etc/init.d/${startup}. Start it? Y/n ? "
#waiting for user answer
read answer
#if answer Y or y
if [ $answer = "y" -o $answer = "Y" ];
then
echo "Starting service..."
#running startup script
/etc/init.d/${startup} start
fi
#exit of if loop
fi
#exit of if loop
fi
Just tested this script it run amazingly ! Thank you so much !
Do you know how can I modify this script so if a service doesn't exist (for example the service "s") the script is currently saying "the service is running" maybe because it's looking for something similar and find the "ssh" service and it takes the "s" so it says running any suggestions ?