Script for validation

Hi,

I need a script which do below

I have one process which runs 24*7 daily and I always has to monitor it now i want to have this automated such that if it stops at anytime we get a exception mail.

For example i use ps -ef|grep 'etl'
above command gives me the desired output.

What i am thinking is like to run this script using cron which will run in evry 5 minutes.

So i need a validation script which works like if my process stops it will throw an exception via mail and will be able to start it immediately.

Please advise.

Many thanks in advance

I would try this:

ps -ef | grep test | grep -v grep
if [ $?==1 ] then; echo "test does not work" |mail -s "confirmation" my@address.com; fi

But I am sure there is more elegant way :slight_smile:

1 Like

Hi Thanks for the suggestion.

I would try this. Meanwhile could you please help me in understand the command you use below

ps -ef | grep test | grep -v grep

ps - ef
  • lists processes
grep test
  • print output lines that contain "test"
grep -v grep 
  • excludes the "grep test" process you ran earlier from the list
1 Like

Hi,

I tried run your script but it is giving me below error

 
: syntax error at line 5: `fi' unexpected

Please help

Your script should look something like this :

#!/bin/bash

#Get the number of processes containing etl running
VAR=`ps -ef | grep 'etl' | grep -v grep | wc -l`

#Verify if VAR >= 1
if [ $VAR -lt 1 ]; then
        echo "Problem -  No etl process running" | mail -s "No process Alert" email@address.com
fi

#END
1 Like

Excellent It worked.

Thanks for your help

---------- Post updated at 09:15 AM ---------- Previous update was at 08:01 AM ----------

Hi Could you please help me to understand the script you made

Well it is quite a simple script. But i'm also a begineer and I will explain it for you:

  • First we declare a variable VAR that computes the number of processes running from the process listing( ps -ef ) that includes "etl" in their name (by using command substitution on bash ` .. `)
    Here we use grep -v grep to exclude the search itsel since a new process will be spawned for grep 'etl' and we don't want this to be counted.
    Then we count the remaining lines by using wc -l
    At this point our VAR variable will hold the number of processes running.

  • We check if the number of process obtained is lower than 1...basically meaning that is 0 which tell us that no process is running.

  • If this is true than we send an email, if not we do nothing.

Hope it helps you to understand it better now.