Monitor , Log , Start or Stop Service

Imagine a situation, we need to monitor some serivce and need to write log about it. Also do some event if , service is running and do something else if service is not running.

This is basic requirement of many sysadmin . So, let's understand this with an example of redis . We want our redis service to keep on running and if it stops, we want to "start" service again .  I have created small script for it, you can use for your own purpose too. Create a file with name "monitor-redis.sh" and write below code. If you are not sure, how to create file, i will recommend go to this tutorial :- http://linux.w3clan.com/tutorial/122/file-management or you can open your favorite text editor and do so too.

#!/bin/bash

#create running time variable

RUNNINGTIME=$(date +"%Y-%m-%d-%H:%M:%S")

getRedisProcessId() {

       # It will return the process id of the redis service

       echo `ps -fe | grep 6379 | grep -v grep | tr -s " "|cut -d" " -f2`

}

# Now, let's log and start or stop if not running.

logStartStopMonitor(){

        # check if redis server is running or not

        pid = $(getRedisProcessId)

        if [ -n "$pid" ]; then 

           echo -e "\e[00;32mRedis running Process ID : $pid\e[00m" $RUNNINGTIME
	   

        else 

           echo -e "\e[00;31mRedis is not running\e[00m" $RUNNINGTIME

           #Now as our service is not running, let's go ahead and start service.

	   echo `service redis restart`

        fi
}

# call function to get the process list

logStartStopMonitor

If you don't know, how to code BASH you will probably not understand , what's happening, but , if you know any server side programing langugae, you will be able to understand some part of it like if else etc.,

Now, before i break down and make you understand what is happening, there is one last twist to do and that is to , create our cron job to run our program and log the output . Here is how to run cron job :-

* 5 * * * monitor-redis.sh >> /var/log/monitor-redis.log

 Above command will run monitor-redish.sh file very 5 minute and will write the output of monitor-redish.sh in log file at /var/log/monitor-redis.log file

# Below command actually creates a file name if it is not already written and will output it's content "Hello " to filename.txt
# If file already exists with some content, all content will be lost with beloe command.

echo 'Hello ' > filename.txt

# Below command will append content "Hello " to filename.txt and will not over write or erase the old content

echo 'Hello ' >> filename.txt

# If file has already content, it will not over write but it will write the content at end of file

Now, let us understand our function above :-

  1.  We created variable RUNNINGTIME with value of date and time. This will be used , so that we know, when command was started and when it gave error if any.
  2. We created function getProcessID , and inside this function we are actually running process checking command, greping for 6379 number and getting the pid , i.e, process id
  3. We create another method logStartStopMonitor inside which check if process id pid exists or not. if it doesn't exist than first echo the output , log is not running which will get logged to our log file running via cron job and start the service. But it pid exists than log the output , redis is running and last time when redis ran.

This is all, if you have question ask in Query box.


Loading ...

Related Results :

  1. Monitor , Log , Start or Stop Service
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author