[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch script to monitor service

Posted on 2012-09-10
4
Medium Priority
?
752 Views
Last Modified: 2012-09-24
Hi,

I try to do some monitoring of a few services and see that the are running and if they are not have them restarted or started. I have a base as below bit it just does not want to work. In addition I try to expand it to add a few more services.

Basically I want to monitor amavisd, postfix, dovecot, spamassassin, httpd and mysql

If any of them is down I want to try to restart up to 3 times and if there is a issue starting them send a email.

Currently I have the following:

#!/bin/bash
 
email=info...
service=amavisd
count=0
threshold=2
servicelog=/var/log/$service.log
 
#checking if service is running
ps -e | grep $service > /dev/null
servicestat=$(echo $?)
 
#if service not running lets try restart 3 times
if [ "$servicestat" != 0 ]; then
 
      while [ "$count" -le "$threshold" ]
      do
            #attempt to start the service
            /etc/init.d/$service start >> $servicelog 2>&1
            if [ $? != 0 ]; then
                  ((count=count+1))
            else
                  exit 0  #if service started exit
            fi
      done
      #if service could not be started, notify
      cat $servicelog | mail -s "problem starting $service" $email 2>/dev/null
fi
 
#END

If anyone could assist me to at least find out what is wrong here. I run a RHES 6 64bit. It does not give any error but does not start the service when I shit it down.

Any ideas?

Best wishes,
Thom
0
Comment
Question by:Thomanji
  • 2
4 Comments
 
LVL 6

Expert Comment

by:aadi369
ID: 38382177
Example of windows management services.

C:\>sc query winmgmt | find /I "running">nul && echo winmgmt is running
winmgmt is running

C:\>sc query nosuchname | find /I "running">nul || echo nosuchname is NOT running
nosuchname is NOT running


set servicename=winmgmt
set message=NO
sc query %servicename% | find /I "running">nul && set message=YES
0
 
LVL 6

Accepted Solution

by:
g3nu1n3 earned 2000 total points
ID: 38382838
Script works perfectly fine. System tested on:

[root@srv ~]# uname -a
Linux srv.....com 2.6.32-279.5.2.el6.x86_64 #1 SMP Fri Aug 24 01:07:11 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@srv ~]# cat /etc/redhat-release
CentOS release 6.3 (Final)
[root@srv ~]#

Open in new window


I received email:

Delivered-To: ...@gmail.com
Received: by 10.58.4.83 with SMTP id i19csp169163vei;
        Mon, 10 Sep 2012 05:51:45 -0700 (PDT)
Received: by 10.60.13.3 with SMTP id d3mr14106050oec.118.1347281504762;
        Mon, 10 Sep 2012 05:51:44 -0700 (PDT)
Return-Path: <root@srv.....com>
Received: from srv.....com (...-Houston.hfc.comcastbusiness.net. [...])
        by mx.google.com with ESMTP id fy3si17524214obc.207.2012.09.10.05.51.44;
        Mon, 10 Sep 2012 05:51:44 -0700 (PDT)
Received-SPF: neutral (google.com: ... is neither permitted nor denied by best guess record for domain of root@srv.....com) client-ip=...;
Authentication-Results: mx.google.com; spf=neutral (google.com: ... is neither permitted nor denied by best guess record for domain of root@srv.....com) smtp.mail=root@srv.....com
Received: by srv.....com (Postfix, from userid 0)
	id 07D24A0072; Mon, 10 Sep 2012 07:51:43 -0500 (CDT)
Date: Mon, 10 Sep 2012 07:51:43 -0500
To: ...@gmail.com
Subject: problem starting sample
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20120910125144.07D24A0072@....com>
From: root@....com (root)

./smon: line 15: /etc/init.d/sample: No such file or directory
./smon: line 15: /etc/init.d/sample: No such file or directory
./smon: line 15: /etc/init.d/sample: No such file or directory

Open in new window

0
 

Author Closing Comment

by:Thomanji
ID: 38389414
Hi g3nu1n3,

Thank you for testing and informing me. It was actually my system that did not send the mail. I have resolved it. Thank you for taking the time to look into this for me.

Best wishes,
thom
0
 

Author Comment

by:Thomanji
ID: 38428190
Hi,

Just to follow up on this. I still did not get it to run on my server.
The reason was on the way cron executes scripts and the way amavisd runs.
I have it working now and here is the script that does it:

#!/bin/bash
#purpose: checking running services

self=$(basename $0)
email=info@...
service=amavisd
count=0
threshold=2
servicelog=/var/log/$service.log
 
#checking if service is running
ps -e |egrep -v $self | grep $service
echo "return code: $?"
 
#if service not running lets try restart 3 times
if [ "$servicestat" != 0 ]; then
 
      while [ "$count" -le "$threshold" ]
      do
            #attempt to start the service
            /etc/init.d/$service start >> $servicelog 2>&1
            if [ $? != 0 ]; then
                  ((count=count+1))
            else
                  exit 0  #if service started exit
            fi
      done
      #if service could not be started, notify
      cat $servicelog | mail -s "problem starting $service" $email 2>/dev/null
fi
 
#END
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question