Monitor log file

Hi,

I need to amend an existing ksh script so that it runs a process (stop weblogic) and in parallel needs to monitor a log file (startup.log) in the background for a certain string (e.g. unable to stop weblogic). If the string appears in the log i need to kill the stop weblogic process.

Currently the script just runs the stop weblogic process and hangs if a certain string appears in the log file.

How about running stop in background and monitoring the log+child-process in the foreground like this:
(assumption is you have /proc filesystem, if not might need to call ps instead of checking the /proc/$CHILD directory).

stop weblogin &
CHILD=$!
while ! grep -q "unable to stop weblogin" startup.log && [ -d /proc/$CHILD ]
do
   sleep 5
done
grep -q "unable to stop weblogin" startup.log && [ -d /proc/$CHILD ] && kill $CHILD

You could also add a saftey check, eg if after X loops it kills the child anyway (for taking too long).

I will give this a try. Many thanks Chubler_XL!

---------- Post updated at 11:07 AM ---------- Previous update was at 08:27 AM ----------

Ok, this is a little more complicated then i thought.

Unfortunately i can't just grep the startup.log for the string "unable to stop weblogin", i need to tail the startup.log everytime my script is run as i need to ignore any previous instances of this string in the log.

Therefore i need to able to run 2 processes simultaneously

Process 1) Tail the startup.log for "unable to stop weblogin"
Process 2) Stop weblogic

and If process 1 finds the string before process 2 has completed I need it to kill process 2

...and if process 2 completes with process having found the string i need my script to kill process 1.

Any help would be much appreciated.

Could you age the log such that the current log only applies to the current instance. This would be normal practice.

That would definately make sense but unfortunately i am unable to change that process. The startup.log is archived per start/stop and therefore i have no choice but to 'tail -f' it.

You could use awk to only look for new occurances of string within log:

(first line creates logfile if none exists - this simplifies rest of code as we can ignore file not existing case).

[ -f logfile ] || > logfile
LN=$(wc -l < logfile)
stop weblogin &
CHILD=$!
 
while [ -d /proc/$CHILD ] && awk "NR>$LN && /unable to stop weblogin/{exit 1}" logfile
do
   sleep 1
done
[ -d /proc/$CHILD ] && ! awk "NR>$LN && /unable to stop weblogin/{exit 1}" logfile && kill $CHILD
1 Like