Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitoring directory

Status
Not open for further replies.

MaxEd

Technical User
Jul 3, 2002
407
US
Hi,

I am new to scripting so please forgive the ignorance that I present here. I am trying to monitor a file that gets placed in a particular directory. The file gets replaced daily when the system runs a process. I want to have a process that stays running to monitor the directory to create a copy of the file to a different directory for archiving. I want to also be able to turn on and off the script. Thank you.
 
You want a filechecking daemon for one file?
Seems a little excessive.

Why don't you just run a cron job using
find to verify your file every half-hour
or so and move it if necessary?

If you really need a daemon I suggest you
look at tcl and tclX because these make
it more simple to write a small program that
could do this for you, as compared to the
shell.
 
I have to agree with MARSD. We just need more detail so we can help. For example, does the file always exist so it is overwritten "anytime" during the day? Can you delete the file from the directory when you are done saving it? Do you control the process that puts it there? (My guess is no or you would not have the question, but I want to be sure.)

Any other detail is only helpful.

-Cheers.
 
marsd,

I guess checking every half hour would be a good idea. I have no control over the output file location or naming. So basically on a daily basis the file can be overwritten maybe 2 times at most but it takes about an hour to run the entire process.

gammerland,
So to answer your question, no I do not have control of the process or else I would have output it with a date stamp and a counter as each file is created from the process.
 
Start by doing this:
touch .timestamp

This just put the current time and date onto a hidden file called "timestamp". We hide it only to prevent it from being deleted later.
Short script:
#!/bin/ksh
#
TIMESTAMP='YourPathAndFilenameToTimestampFile'
FILENAME='yourpathandfilenamehere'
ARCHIVE=YourArchivePathAndNameHere'
find $FILENAME -newer $TIMESTAMP -exec cp $FILENAME $DESTINATION {} \; -exec touch $TIMESTAMP {} \;
---
What this says: Find any file newer than the timestamp and copy it to the destination. When you are done, we touch the timestamp again, so it is waiting for the next file.

If we forget the touch, we will copy the same file on each run.

You can then put it into cron to run as often as you wish. Given the limited scope of this specific find have it run as often as is possible for the file to arrive.
 
gamerland,

How do I attach a date stamp to the file so that when I archive that file I give it a unique name and date so that I can track when they were archived?
 
Let's assume you have a file called bob.log

Short script:
#!/bin/ksh
#
TIMESTAMP='YourPathAndFilenameToTimestampFile'
FILENAME='yourpathandfilenamehere'
FILESTAMP=`date '+%m%d.%H%M'`
DESTINATION='YourArchivePathAndNameHere.$FILESTAMP'
find $FILENAME -newer $TIMESTAMP -exec cp $FILENAME $DESTINATION {} \; -exec touch $TIMESTAMP {} \;

This will add a date/time at the end of the format 1231.0901 to the end of the file name.
 
Short script:
#!/bin/ksh
#
TIMESTAMP='YourPathAndFilenameToTimestampFile'
FILENAME='yourpathandfilenamehere'
ARCHIVE=YourArchivePathAndNameHere'
touch -t $(date +%Y%m%d0000) $TIMESTAMP
find $FILENAME -newer $TIMESTAMP -exec cp $FILENAME $DESTINATION {} \;
---
What this says: Find any file newer than the timestamp and copy it to the destination. Replace the copy with whatever you wish to do with the files found.
 
gamerland,

For my timestamp file, do I just create a blank file?
 
gamerland,

What is the ARCHIVE variable used for? It doesn't seem to do anything also the DESTINATION variable is not being used? I'm new to UNIX scripting so please bare with me.
 
MadEx,
You will not need to do anything except give it a name. When "touch" happens it will create a zero byte file with using the timestamp file name you provide.
 
"D'oh!" Bad cut/paste/correct.

#!/bin/ksh
#
TIMESTAMP='YourPathAndFilenameToTimestampFile'
FILENAME='yourpathandfilenamehere'
ARCHIVE=YourArchivePathAndNameHere'
touch -t $(date +%Y%m%d0000) $TIMESTAMP
find $FILENAME -newer $TIMESTAMP -exec cp $FILENAME $ARCHIVE {} \;
---
 
gamerland,

Thanks! Too bad the IS department wouldn't let me do any cron commands. Oh well thanks for the UNIX lesson.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top