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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run this grep every 10 minutes and do something based on the output
# 1  
Old 12-22-2014
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.

Code:
# 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. Smilie

Any idea how I can write this piece of code ?
# 2  
Old 12-22-2014
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:-
Code:
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:-
Code:
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
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 12-22-2014
Try this (too)
Code:
while true; do #infinite loop
 if grep -v Processing status.txt &>/dev/null; then
    echo Complete
    exit
 fi
 sleep 600
done

This User Gave Thanks to junior-helper For This Post:
# 4  
Old 12-22-2014
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
These 2 Users Gave Thanks to rbatte1 For This Post:
# 5  
Old 12-22-2014
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 ?
# 6  
Old 12-22-2014
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:-
Code:
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
This User Gave Thanks to rbatte1 For This Post:
# 7  
Old 12-22-2014
It's simply a matter of taste. I used it to keep the terminal clean(er) Smilie
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 ----------

Quote:
Originally Posted by rbatte1
The redirect will stop you getting messages to standard output ...
It covers standard error, too Smilie
These 2 Users Gave Thanks to junior-helper For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file for the last 5 minutes of contents every 5 minutes

Hi all, System Ubuntu 16.04.3 LTS i have the following log INFO 2019-02-07 15:13:31,099 module.py:700] default: "POST /join/8550614e-3e94-4fa5-9ab2-135eefa69c1b HTTP/1.0" 500 2042 INFO 2019-02-07 15:13:31,569 module.py:700] default: "POST /join/6cb9c452-dcb1-45f3-bcca-e33f5d450105... (15 Replies)
Discussion started by: charli1
15 Replies

2. Shell Programming and Scripting

Cron Job to Run every 2 minutes

Hello Gurus, I want to schedule a cron job which will run every 2 minutes starts at 11:25 AM and 3:25 AM daily. Can you please suggest as how to schedule the job. Thanks- Pokhraj Das (2 Replies)
Discussion started by: pokhraj_d
2 Replies

3. Shell Programming and Scripting

Different output based on run method

Sample Data #cat /mylist.txt /raid/job/123 /raid/job/456 /raid/job/789 # Script #cat /myfind.scr #!/bin/bash touch /nojob.txt touch /job.txt unset dirfolder for dirfolder in `/bin/cat /mylist.txt`;do if then echo $dirfolder >>/job.txt else echo $dirfolder >>/nojob.txt (3 Replies)
Discussion started by: crowman
3 Replies

4. Shell Programming and Scripting

Output block of lines in a file based on grep result

Hi I would appreciate your help with this. I have a output file from a command. It is broken based on initial of the users. Exmaple of iitials MN & SS. Under each section there is information pertaining to the user however each section can have different number of lines. MY challenge is to ... (5 Replies)
Discussion started by: mnassiri
5 Replies

5. Shell Programming and Scripting

Run cronjob for every 10 minutes

Hi Friends, I have a requirement to run the cronjob for every 10 minutes from 2:00 AM to 6:00 AM. Does the below code works? If not, please advise. * * * * * command to be executed ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └---------------------------------- day of week (0 - 6) (0 is... (5 Replies)
Discussion started by: srikanthbiradar
5 Replies

6. Shell Programming and Scripting

Run a script continuously for 10 minutes

Hi all!! Im using ksh and my OS is Linux. I want to run a script for ten minutes, starting from my current system time. How to acheive this? Any help appreciated. Thanks in advance (5 Replies)
Discussion started by: Jayaraman
5 Replies

7. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

8. UNIX for Dummies Questions & Answers

Run the shell script for every 15 minutes?

I want to run my shell script for every 15 minutes starting from 12:20AM. I am passing parameter GA to shell script. Does this work? Any one please comment on this? 20 0-23/15 * * * xyz.sh 'GA' > xyz.log 2>&1 (9 Replies)
Discussion started by: govindts
9 Replies

9. Shell Programming and Scripting

Script to run every 5 minutes

Hello all, I want to run a script every 5 minutes. How to accomplish this task? Thanks in advance Mrudula (12 Replies)
Discussion started by: mrudula009
12 Replies

10. Solaris

Schedule to run every 3 minutes - CRONTAB

Hello all, I want to run a script every 3 minutes in os level and to send mail. I scheduled in crontab as 3 * * * * /mnt1/monitorscripts/testdbstart.sh I got mail every one hour and I confirmed that the script is running every 1 hour which doesn't meet my requirment. Where I am... (4 Replies)
Discussion started by: prashanth_gs
4 Replies
Login or Register to Ask a Question