1748181 Members
3965 Online
108759 Solutions
New Discussion юеВ

timestamp script

 
SOLVED
Go to solution
sahaj bahra
Frequent Advisor

timestamp script

I have written a small script which will monitor for a specific word in a text.log file I need to run this script in such a way that it runs only if text.log is a new file.
This text.log file keeps updated with the new one and doesnot gets appended.
So will need a script which will check if the file is new then goes ahead with rest of the action.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: timestamp script

Hi:

THis is one way to do what you seek:

# cat ./watch
#!/usr/bin/sh
LOGF=/tmp/mylog
REFF=/tmp/myref
touch ${LOGF} ${REFF}
while true
do
if [ "$LOGF" -nt "${REFF}" ];then
echo "${LOGF} has been updated"
touch -cmr ${LOGF} ${REFF}
fi
sleep 30
done

...You can play with this concept by running the script in the background and 'touch'ing the LOGF file to update it.

Adjust the script's wleep time to you needs.

Regards!

...JRF...
Ken Martin_3
Regular Advisor

Re: timestamp script

Sonal,

James has a good idea.

I have a question though and that is how often is the log file replaced? Is it a known time interval or random interval?

If it is random what is the shortest time?

You want to set your sleep time shorter then the shortest time interval but not so short that it is running all the time. That can hog CPU time.

Is this being run from a terminal (telnet) session or in the background? If you are running this in the background from a telnet session you may want to add a 'trap' statement (example trap exit 1 2 3 15) in the beginning to stop the script if the telnet session terminates.

-Ken
James R. Ferguson
Acclaimed Contributor

Re: timestamp script

Hi:

> Ken: If you are running this in the background from a telnet session you may want to add a 'trap' statement (example trap exit 1 2 3 15) in the beginning to stop the script if the telnet session terminates.

This doesn't matter to the job in the background if it was started with 'nohup'. In that case, the shell will terminate and any running children will be inherited by the 'init' daemon making 'init' their parent process.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: timestamp script

Shalom,

find $DATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST

That will pick up files that are new.

Then process the LIST and take whatever action you need.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: timestamp script

Hi (again):

@ SEP: If you read the OP's description it notes, "I need to run this script in such a way that it runs only if text.log is a new file.".

The interest is in ONE particular file, not a list of files that have been updated. The goal is for the monitoring script to be awaken when that file changes; to note that change event; and sleep until another change occurs. Subsequent changes could be seconds, minutes, hours or days later. Your suggestion doesn't help that objective.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: timestamp script

>JRF: touch ${LOGF} ${REFF}

That's: touch -r ${LOGF} ${REFF}

James R. Ferguson
Acclaimed Contributor

Re: timestamp script

Hi:

> Dennis: touch -r ${LOGF} ${REFF}

Yes, good point, Dennis. I didn't mean to update the timestamp of the ${LOGF} at startup. Thanks.

Regards!

...JRF...
sahaj bahra
Frequent Advisor

Re: timestamp script

Thanks Guys