Operating System - HP-UX
1832857 Members
3592 Online
110048 Solutions
New Discussion

Scripting query... Monitoring a directories contents.

 
Russ Hancock_1
Frequent Advisor

Scripting query... Monitoring a directories contents.

I need to write a script that acts upon a file when it appears in a directory...

In the past I've used a periodic cron process that checks the directory every 1min or so... but my question is, is there a better way of doing this?

Cheers
Russ
Russ
5 REPLIES 5
G. Vrijhoeven
Honored Contributor

Re: Scripting query... Monitoring a directories contents.

Hi Russ,

You have tools like tripwire that van do that for you.

check:

http://www.tripwire.org/


those tools do not consume that much CPU time.

Regards,

Gideon
Mark Grant
Honored Contributor

Re: Scripting query... Monitoring a directories contents.

This may not be too helpful but it's always best to somehow get the process creating the file to tell you it's been created rather than you writing something to scan a directory all the time. I realise it's not always possible but it's worth being creative about if you can.
Never preceed any demonstration with anything more predictive than "watch this"
Russ Hancock_1
Frequent Advisor

Re: Scripting query... Monitoring a directories contents.

Thank Mark, but the file will be comingin from ftp..
Russ
Mark Grant
Honored Contributor

Re: Scripting query... Monitoring a directories contents.

Russ,

Can the machine that is ftping to you either rexec/ssh the receiving machine to kick of a process. Maybe it can mail an account and you get "procmail" to start off the processing.

One thing that probably doesn't work but might be fun to try is to pre-create the files as named pipes and have a process read the named pipes. Of course you would need to know what the file names are going to be and you need to persuade ftp to not break a named pipe when it transfers a file to it. However, if it works, it means no unnecessary load and I like that :)
Never preceed any demonstration with anything more predictive than "watch this"
Clay Jones_2
Frequent Advisor

Re: Scripting query... Monitoring a directories contents.

Russ,

What would "better" entail?

The only thing you could change apart from checking every 1 minute via cron would be to check every few seconds with some sort of daemon script:

#!/usr/bin/ksh

while true
do
if [[ -a ${file} ]]
then
process_file
fi
sleep 10
done

This script would run as a process until someone killed it. It would check more frequently.