Code to search for a phrase and run a command

hi All,

Am newbie to unix and would require your help to complete this task.

Condition:
I have a server start up script and I would like to append the code in such a way that it searches for a phrase "process completed" in taillog when server starts up and also should run another script abc.sh as soon as it sees the above message(process completed).
OS: SunOS 5.10 Generic_142900-14 sun4u sparc SUNW,Sun-Fire-V245

Thanks in Advance.

So it needs to continuously monitor a growing file?

No, not a monitoring thing, I want to put this piece in server script, such that whenever server is restarted, while in restart process, this code should search for "process completed" phrase in taillog and when it finds that phrase, should run one more script xxx.sh from a particular location.

Looking for something like this? This works on Linux, can't confirm the same on SunOS.

find . -name taillog -exec grep -q 'process completed' {} \; -exec ./xxx.sh  \;

for some reason above statement which mjf sent isn't working, as taillog is not a file, it is a log file. I somehow managed to google and found this one.

tail -f /a/b/c/xyz.log | grep 'process completed'

above statement was successful as I just tested with my requirements. So I am looking for second part which means a command should run a script as soon as it finds 'process completed' phrase in xyz.log.

Use awk instead of grep which then can exit when it finds the desired string:

set -o pipefail
tail -f /a/b/c/xyz.log | awk '/process completed/ { exit( 0 ) }'
if (( $? != 0 ))
then
   echo "abort: tail or awk failed"
   exit 1
fi
echo "execute desired comand"

The awk will "block" and exit when it finds the desired string. After that you should check the return code and then can run your command if the tail/awk pipe was successful.

---------- Post updated at 21:52 ---------- Previous update was at 21:48 ----------

You could also use the -lq (lower case L) on grep which should have the same effect:

tail -f /path/file | grep -lq "process complete"

Awk always springs to mind first for me!

hi agama,

got one more question here, as my xyz.log file keep changing daily as it has date stamp in filename just like xyz073112.log(as of today). So can we use /a/b/c/taillog.sh in first step in above code. Any means we can use taillog.sh to get rid of daily updated xyzxxx.log file

In Solaris, /usr/bin/grep does not support -q flag. Try to use /usr/xpg4/bin/grep, see man page for details.

You need to test the return status ($?) for the /usr/xpg4/bin/grep 'cos it does not return anything even if there is a match.

1 Like

Probably. Not sure what taillog.sh does exactly, but assuming that it finds the right log, and then tails it writing to standard output, then yes you should be able to use that in place of tail -f.

I didn't notice that you indicated Solaris, so go with the suggested version of grep, or just use awk.

Appreciate the quick responses, but these things wrecking my mind am completely new to unix. So anyone who can provide exact solution to my requirement. It should be like ...

  1. taillog.sh is the tail log script which keeps updating when my server comes up
  2. phrase to be searched for is "process completed"
  3. once code find the above phrase(process completed) in taillog.sh , it should execute another ./startscript.

thanks in Advance

---------- Post updated 08-01-12 at 10:09 PM ---------- Previous update was 07-31-12 at 10:15 PM ----------

Can any one help out with a script with below requirement.,