1

I have this bash script and inside the script, I am trying to grep a log for the words "status change" and then after I find the most recent line that contains the words "status change" (aka the most recent status change), I grep that line for the word "Backup".

The problem is that I am currently putting it to sleep for 15 seconds first so the log has time to be updated of the most recent status change but I want a better solution for this.

I want it to actively check for the status and report a failure only after 15 seconds. If possible, I would like to only replace my first 2 lines of code because I plan on making other modifications to the rest. Any help would be greatly appreciated! Here is what I currently have:

 sleep 15 ssh server_one@$1 "grep 'status change' /apps/tests/$2 | tail -1 | grep 'Backup'" Output=$? if [[ ${Output} -ne 1 ]] ; then echo "Pass: It is Backup" echo "Pass: It is Backup" >> $REPORT else echo "Fail: It is not Backup" echo "Fail: It is not Backup" >> $REPORT fi 
2
  • The text of the question talks about looking for the word active. The code looks for the word Backup. Which is it? Commented Jul 20, 2016 at 22:40
  • The two most obvious solutions that occur to me are: 1. if the remote system is running linux, use inotifywait from the inotify-tools package; and 2. use the perl File::Tail module. Both methods avoid any need to sleep. Commented Jul 21, 2016 at 8:53

1 Answer 1

0

Answer intended for original version of question

For your one command, replacing the grep-tail-grep-if commands, try:

awk '/status change/{last=$0} END{printf "It is %sBackup\n",(last~/Active/?"":"not ")}' "/apps/tests/$2" 

How it works:

  • /status change/{last=$0}

    Every time that a line containing status change is found, it is saved in the variable last.

  • END{printf "It is %sBackup\n",(last~/Active/?"":"not ")}

    This is executed after we have finished reading the file. At that time, last will contain the contents of the last line that contained status change. If that line also contains Active, then we print It is Backup. Otherwise, we print It is not Backup.

    The presence or absence of the word not is controlled by the somewhat complex looking ternary statement: (last~/Active/?"":"not "). This statement just checks to see if last~/Active/ is true (meaning the line last contains Active). If it does, the ternary statement returns the empty string "". If it does not, it returns the string "not ".

Continuous monitoring

To monitor continuously:

ssh "server_one@$1" tail -f "/apps/tests/$2" | awk '/status change/{printf "It is %sBackup\n",($0~/Backup/?"":"not ")}' 
3
  • thank you for your help! instead of replacing my entire scripts above, would it be possible to only modify the line 'ssh server_one@$1 "grep 'status change' /apps/tests/$2 | tail -1 | grep 'Backup'" '? I am planning to modify the code so it becomes 'echo "Pass: $1: $2 is Backup" echo "Pass: $1: $2 is Backup" >> $REPORT ' In addition, I meant to say to grep for the word "Backup" instead of "active" so sorry about that. Commented Jul 21, 2016 at 16:43
  • In addition, I was wondering where in the code you wait until 15 seconds and if you still can't find it, you report it to not be backup? Commented Jul 21, 2016 at 22:08
  • @saraberry OK. I added a section that avoids the 15 seconds problem and monitors continuously. Commented Jul 22, 2016 at 20:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.