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.?
# 15  
Old 04-12-2013
I don't know how large your log files will be, but greping or seding them will always read them from the start, which may become serious overhead.
If you just want to know if there's a DONE occurred in your log file ever since your last visit, why don't you keep the recent file size in a variable:
Code:
$ skip=$(stat -c%s file)

, and then, n seconds later, check the lines added ever since with
Code:
$ dd if=file ibs=1  skip=$skip | grep DONE

. You'll always have exactly the last chunk of data added with no overhead!
See the time differences for a 200MB log file::
Code:
$ time grep -n DONE file |tail -1
7040081:DONE

real    0m0.653s
user    0m0.516s
sys    0m0.132s
$ time dd if=file ibs=1 skip=$skip |grep DONE
DONE

real    0m0.005s
user    0m0.000s
sys    0m0.004s

dd is standard for every *nix implementation while stat may not be; then ls and awk will be your friends...

Last edited by RudiC; 04-12-2013 at 07:14 PM..
These 2 Users Gave Thanks to RudiC For This Post:
# 16  
Old 04-15-2013
Going above and beyond w/ efficiency :)

Hi RudiC-

Wow, I was really excited to see this idea too since I'm a big fan of efficiency.

Unfortunately, as you mentioned may be the case, "stat" isn't available in my environment (Solaris).

I'm not as familiar with dd (it appears I need "stat" beforehand to make it use of "dd" ? ).

If that's true, perhaps I can combine your method with Hanson's way of getting the (original) last line with done:
Code:
LAST_LINE_WITH_DONE=`grep -n DONE log | tail -n 1 | cut -d : -f 1`

....then, perhaps there's some way of modifying the below line:
Code:
NEWLAST_LINE_WITH_DONE=`grep -n DONE $LOGFILE | tail -1 | cut -d : -f 1`

...so that it can start searching the file after line number $LAST_LINE_WITH_DONE ?

In the worst case, this is more of a fun challenge to make things more efficient, since Hanson's idea (ps: thanks again!) has been working very well too.

Thanks again everyone!
CG
# 17  
Old 04-15-2013
As I stated in my post, you don't NEED stat; it's just more efficient than this method to get the files sizes in bytes:
Code:
$ ls -l file newaa
-rw-rw-r-- 1 xxxxx xxxxx 1300 Apr 14 22:26 file
-rw-rw-r-- 1 xxxxx xxxxx  111 Apr  7 22:37 newaa
$ ls -l file newaa | awk '{print $5}'
1300
111
$ stat -c%s file newaa
1300
111

The result ist the same, as you can see. dd just has to skip last time's file sizes' count of bytes to get at the new lines added.
I could imagine that Solaris has a stat equivalent command, btw, and you just need to search a bit...

And, the two methods would be difficult to combine, as dd needs a number of bytes or blocks to skip, and grep identifies lines that can be of varying length. Would sort of work if one could assume an average line length, but this were far from being exact.

Last edited by RudiC; 04-19-2013 at 05:50 PM..
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