Wednesday, February 25, 2015

Check if service is running or not on Redhat servers

[dev@localhost ~]$ cat  http.check.ksh

#!/bin/ksh
# Checks if particular servce is running or not.
#
###### config_file ######
#-----------------------#
#### cat hosts_list ####
######## server1 ########
######## server2 ########
#
USER_CHOICE=$1
HOSTS_LIST=$2
# APP_CHK=httpd

if [ -z $USER_CHOICE ]
then
    echo usage: your_script.ksh --check  {hosts_list_config_file}
    exit 0
fi
if [ -z $HOSTS_LIST ]
then
    echo usage: your_script.ksh --check  {hosts_list_config_file}
    exit 0
fi
if [ ! -f $HOSTS_LIST ]
then
    echo file $HOSTS_LIST does not exist. Pls add to the configuration.
    echo please check the config file and try again
    exit 4
fi
if [ "$USER_CHOICE" = "--check" ]
then
    for i in $(cat $HOSTS_LIST)
    do
       # /usr/bin/uptime > /dev/null 2>&1
       # if [ $? -eq 1 ]
       # then
       #         echo something wrong >> failed.log
       # else
 
        if [[ "$(service httpd status | grep httpd | awk -F" " '{print $3}')" = "stopped" ]]
        then
                echo httpd is stopped
        else
                echo httpd is running.
        fi

    done
else
    # no valid choice
    echo usage: your_script.ksh --check  {hosts_list_config_file}
fi
[dev@localhost ~]$

Config file
[dev@localhost ~]$ cat hosts_list
localhost.localdomain

To run the script
[dev@localhost ~]$ sh http.check.ksh --check hosts_list

No comments:

Post a Comment