How to have my ksh script pause, until something appears in the logs.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to have my ksh script pause, until something appears in the logs.?
# 8  
Old 04-11-2013
Would this work:

The first time, check if "DONE" already exists in the log file, using grep -n and tail -n 1 to find the last line number that "DONE" occurs on.

Then use the while loop, sleeping every ten seconds, but tail the log file each time you look, instead of looking at entire log file. That way, you ignore the previous "DONE" lines at the top of the log file that were already there,
This User Gave Thanks to hanson44 For This Post:
# 9  
Old 04-11-2013
Are there no timestamps in this log file?

Or do as you suggested and tail the file. Pipe the stream into code that exits when it matches (grep -q). Worst case, tail will linger until the next time it tries to write (when SIGPIPE will kill it).

Regards,
Alister

Last edited by alister; 04-11-2013 at 05:23 PM..
This User Gave Thanks to alister For This Post:
# 10  
Old 04-11-2013
is it necessary to key off of the "DONE!" or could you somehow have the script check the status of the software under test? maybe running some sort of pre-existing status command or check the system's process list if the readiness of the SUT can be determined that way. (the check could be in a loop that would terminate as soon as it pro-actively determined the "MYSQL DB has bound to the RMI/Software" - if that is possible w/o having to monitor a log for status)
just trying to think of other possible options that might be cleaner for you...
biker
www.LiveFireLabs.com
This User Gave Thanks to biker For This Post:
# 11  
Old 04-12-2013
Replies to Hanson, Biker & Alister & more info on how to solve...

Hi Hanson:

Hm, I think you're on to something. I'll admit, I'm not sure how to "tail in the background" for every 10-seconds, if that is what you mean. (As you can tell, I'm probably somewhere between a novice and intermediate KSH scripter)

But your grep -n idea got me thinking:

Perhaps one way of scripting this is: (?)

1) Shutdown software ("cControl stop")
2) grep -n for DONE! (using awk?, record the "latest" line number that DONE! was found on, or record "0" if not found.)
3) Startup software ("cControl start")
4) In a while loop, every 10 seconds, grep -n for DONE!
4b) Break while loop (somehow?) if grep -n finds a "LATER" line number than what was recorded in step 2 above. <-- Hm, not sure how to do this...but it sounds possible?

Hi Biker:

Very good point that I wish I would have thought of in the design phase! Unfortuanately it's too late to add to the design (as much as it would make things a lot easier), the only way it could be determined though, is with the logs Smilie

Hi Alister:

Yes! There actually are timestamps. This was my first chain of thought, but as I work on this system late (which is also 5 hours ahead in GMT), it's likely I will hit the 00:00:00 midnight hour mid-way through...so I'm not quite sure how to attack it from that angle. Here's a snippet of those logs:
Code:
Apr 11 19:19:39 localhost APP.ph1[16435]: [local4.info] INFO creating Location
Apr 11 19:22:31 localhost APP.ph1[16435]: [local4.info] INFO creating LocDef
Apr 11 19:23:16 localhost APP.ph1[16435]: [local4.info] INFO DONE!
Apr 11 19:30:00 localhost APL.ph0[3102]: [local4.info] INFO APL Heartbeat 2106
Apr 11 19:30:00 localhost APP.ph0[3105]: [local4.info] INFO APP Heartbeat 4074
Apr 11 19:34:40 localhost APP.ph1[16435]: [local4.info] INFO New thread pool of size:3
Apr 11 19:34:41 localhost APP.ph1[16435]: [local4.info] INFO Starting Iteration 1
Apr 11 19:34:43 localhost APP.ph1[16435]: [local4.info] INFO Iteration 1, all tasks finished
Apr 11 19:34:43 localhost APP.ph1[16435]: [local4.info] INFO Running command:/APP/createTar.sh ph1 
Apr 11 19:37:54 localhost APP.ph1[16435]: [local4.info] INFO creating Server
Apr 11 19:41:49 localhost APP.ph1[16435]: [local4.info] INFO creating Site
Apr 11 19:42:05 localhost APP.ph1[16435]: [local4.info] INFO creating Location
Apr 11 19:42:22 localhost APP.ph1[16435]: [local4.info] INFO creating LocDef
Apr 11 19:44:23 localhost APP.ph1[16435]: [local4.info] INFO DONE!

If you know of any way of attacking it from that angle, that would certainly help as well!


ps: Thank you all for your help so far! Definitely appreciated!

CG

Last edited by Franklin52; 04-16-2013 at 03:08 AM.. Reason: Please use code tags for code and data samples
# 12  
Old 04-12-2013
one thing i did for a tool i wrote a year or two back was to use the epoch value (seconds since 1970) for date/timestamp comparisons if that's the concern

biker
LiveFire Labs: Online UNIX Training with Hands-on Internet Lab
# 13  
Old 04-12-2013
Quote:
1) Shutdown software ("cControl stop")
2) grep -n for DONE! (using awk?, record the "latest"
line number that DONE! was found on, or record "0" if not found.)
3) Startup software ("cControl start")
4) In a while loop, every 10 seconds, grep -n for DONE!
4b) Break while loop (somehow?) if grep -n finds a "LATER"
line number than what was recorded in step 2 above.
<-- Hm, not sure how to do this...but it sounds possible?
Yes, this is exactly what I'm suggesting. Here is working code, using sed as a better way to get the part of log file to test:
Code:
LAST_LINE_WITH_DONE=`grep -n DONE log | tail -n 1 | cut -d : -f 1`
if [ -z "$LAST_LINE_WITH_DONE" ]; then
  LINE_1_TO_TEST=`wc -l < log`
else
  LINE_1_TO_TEST=`expr $LAST_LINE_WITH_DONE + 1`
fi

while sleep 10; do
  sed -n "$LINE_1_TO_TEST,$ p" log > lines-to-test.txt
  grep -q DONE lines-to-test.txt
  if [ $? -eq 0 ]; then
    echo "Found DONE in newly created section of log"
    # Take additional actions
    # Perhaps break out of look
  fi
done

This User Gave Thanks to hanson44 For This Post:
# 14  
Old 04-12-2013
SOLVED

Hansen,

You are a genius! At the same time, I never thought it was this easy to do this! Thank you and everyone who chimed in!!

ps: If the word DONE was not previously in the file, the variable is NULL. Turns out this is just fine, since I can compare null with 1, if the first DONE is added.

Here is the modified code I used:
Code:
LOGFILE="/tmp/tmp.log"

LAST_LINE_WITH_DONE=`grep -n DONE $LOGFILE | tail -1 | cut -d : -f 1`

print INITIAL LAST LINE IS $LAST_LINE_WITH_DONE

while sleep 5; do

  NEWLAST_LINE_WITH_DONE=`grep -n DONE $LOGFILE | tail -1 | cut -d : -f 1`
   if [[ $LAST_LINE_WITH_DONE != $NEWLAST_LINE_WITH_DONE ]]; then
     print NEW LAST LINE IS $NEWLAST_LINE_WITH_DONE
     break;
   fi
done

This User Gave Thanks to chatguy 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

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Pause before exit

6) printf "\n GoodBye! \n\n"; exit ;; I am trying modify the above command to pause a couple of seconds before exiting, so a message can be displayed. Thank you :). (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Creating a script requiring a pause for user input

Hi I'm trying to create a basic script that pauses for user input to verify a file name before generating the output. I have numerous SSL certificate files which I am trying to determine the expiry date so what I'm trying to do is write a script so that is pauses to request the name of the .pem... (9 Replies)
Discussion started by: Buddyluv
9 Replies

4. Shell Programming and Scripting

Pause shell script till folder doesn't change size anymore

Hi, I recently would like to write a shell script that 1. Runs in the background (can be done with "&", but i'd be happy for other solutions to keep programs running) 2. Does its stuff 3 THEN checks a specified folder for a size change over time (say, each 5 seconds. AND ONLY continues with... (9 Replies)
Discussion started by: pasc
9 Replies

5. Shell Programming and Scripting

script for reading logs of a script running on other UNIX server

Hi, I have a script, running on some outside firwall server and it's log of success or failure is maintained in a file. I want to write a script which ftp that server and reads that file and checks the logs and if failure , I will send mail notification. Please let meknow if I am not... (1 Reply)
Discussion started by: vandana.parwani
1 Replies

6. Shell Programming and Scripting

Pause for remote computing in Telnet script

I am running a telnet script that connects to a database server and executes sql scripts. While the remote database is processing the commands within the sql scripts, my telnet script continues to send commands. Okay, that's fine. They get queued up in the remote server and executed sequentially.... (3 Replies)
Discussion started by: oldjuke
3 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

How to pause a shell script

Hi, I've written a shell script to take photos with my camera. After every picture taken, the picture is transmitted to the computer via usb and then deleted on the camera. But sometimes there's an error and the picture is not deleted and so, after a certain time, the camera chip will be... (4 Replies)
Discussion started by: McLennon
4 Replies

9. Shell Programming and Scripting

Script Pause Until Rsync Is Done Transferring

Alright, I have this script that pulls files from a few locations, process those files, creates a zip file, rsync's it and then removes everything. The problem that I'm having is that I do not know how large the rsync'ed zip file is going to be. Right now I'm using a sleep command before I... (4 Replies)
Discussion started by: droppedonjapan
4 Replies

10. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question