Run this grep every 10 minutes and do something based on the output

OS : Red Hat Linux 6.4
Shell : Bash

We have a file called status.txt which will have only 1 line. The content will be the string "Processing" for most of the day.

# cat status.txt
Processing
#

I want to write a shell script (notify.sh) which will be executing a grep every 10 minutes .

Once the script is executed , it should do the following

  1. run a grep command every 10 minutes and check if the content of status.txt is anything other than "Processing" .

  2. If the status.txt file has anything other than the string " Processing " (ie. grep -v "Processing" ) then it should echo "Completed". Actually in my real business scenario If the status.txt has anything other than "Processing" then a mail needs to be send out using Linux's mail command. But, for our testing purpose an echo "completed" should do .

  3. Stop the execution of the notify.sh script (exit) after grep command managed to find anything other than the string " Processing " and printed " Complete " .

Note: I don't this to be invoked via a cronjob because in my real world scenario this logic has to be incorported as a subsection of a much bigger script. :slight_smile:

Any idea how I can write this piece of code ?

Would you be better to schedule this with cron to run every 10 minutes, so that if your session crashes, the process will still fire up? You could easily code it something like:-

myvar="$(cat status.txt)"
if [ "$myvar" = "Processing"
then
   # Take normal action here
else
   # Take exception action here
fi

If the normal action is just to go back to sleep, then this simplifies to:-

myvar="$(cat status.txt)"
if [ "$myvar" = "Processing"
then
   print "Still working" >> trace_file
   exit 0
fi
# Take exception action here

Is this what you are after? If not, could you explain a little more?

Thanks, in advance,
Robin

1 Like

Try this (too)

while true; do #infinite loop
 if grep -v Processing status.txt &>/dev/null; then
    echo Complete
    exit
 fi
 sleep 600
done
1 Like

I would worry that your script will not start again after a server boot, network hiccup or other trauma that stops it, hence why I suggest scheduling it as discreet test rather than the infinite loop.

Robin

2 Likes

Thank You Robin.

I wanted the IF.. THEN.. ELSE block to continue executing every 10 minutes until grep command does not see the the string "Processing" in this file.

Thank You Junior Helper. I think your solution works fine for my requirement. I need to test it in my real life script. THANK YOU.

I was under the impression that the number mentioned for sleep command is in minutes not seconds. Just realized that I was wrong

ie.

sleep 30 means sleep for 30 seconds not 30 minutes .

One more question:
Why are you redirecting the output of grep -v Processing status.txt to bit bucket &>/dev/null ?

The redirect will stop you getting messages to standard output (the screen for an on-line session) that you may not want. You could also dispose of it with:-

while true
do
   if grep -qv Processing status.txt
   then
      echo Complete
      exit
   fi
   sleep 600
done

It is the return code (i.e. did I match the expression/string or not) that is best here, rather than my ill-considered attempt to read the file into a variable each time.

Robin

1 Like

It's simply a matter of taste. I used it to keep the terminal clean(er) :slight_smile:
Sometimes one just doesn't care about the output because we only rely on the exit status, like in this case.
Anyways, &>/dev/null is not mandatory here.

---------- Post updated at 07:19 PM ---------- Previous update was at 07:14 PM ----------

It covers standard error, too :cool:

2 Likes
read myvar < status.txt

is shorter than

myvar="$(cat status.txt)"

---------- Post updated at 01:21 PM ---------- Previous update was at 01:12 PM ----------

And the loop can be written as

while grep Processing status.txt
do 
  sleep 600
done >/dev/null
echo Complete
2 Likes