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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX Background Task - DAEMON ?

Status
Not open for further replies.

dayankoven

Programmer
Oct 28, 2002
17
MY
Hi there fellow experts,

What i want to achieve is to have a script that consistently scans a directory for the arrival of files. Once a file arrives, the script will need to invoke a another program. I believe that we can utilise something called a UNIX DAEMON to achieve what i just mentioned. Would be very grateful if any of the forummers can vouch for me on this and also highlight some sample codes on how i can code this shell script.
Thanks in advance for all the help and hope to hear some good news real soon.
 
> the script will need to invoke a another program
Just run the program, or supply it with a list of the fresh files?

How often are you planning to check for new files?

How are files appearing in the directory? If they are being 'moved' there, then there should be no problem - but if they are being copied or written, there is the potential for various problems should the detector run in the middle of a file being created.


 
>>Just run the program, or supply it with a list of the fresh files?
<<The script will run a program passing in the name of file that has just arrived as an arguement.

>>How often are you planning to check for new files?
<<I think every half an hour should be sufficient

>>How are files appearing in the directory? If they are being 'moved' there, then there should be no problem - but if they are being copied or written, there is the potential for various problems should the detector run in the middle of a file being created.
<<The files are being 'moved' there via FTP.

Appreciate your help and thanks a lot.







 
You can do this in the shell but I would use perl or
tcl/tk.
Here's some partially tested tcl code.

Code:
#!/usr/bin/tclsh
#trivial directory watcher
set interval 15000
set filelist1 {}
set filelist2 {}

package require Tclx

proc openDir {dir flist} {
upvar #0 $flist mylist

      foreach poss [glob -nocomplain $dir/*] {
                  if {[file isfile $poss]} {
                      lappend mylist $poss
                  }
      }
}
 
proc compFiles {} {
global filelist1 filelist2 

     foreach n $filelist2 {
               if {![lsearch -exact $filelist1 $n]} {
                    lappend new $n
               }
      }
if {[llength $new]} {return $new}
}

proc lprint {comment lis} {
 puts -nonewline $comment ; flush stdout
 foreach p $lis {puts $p}
}

proc action {act lis} {

       foreach p $lis {
             eval {exec $act $p}
       }
}
     
proc clrscrn {num} {
set x 0

       while {$x < $num} {
             puts &quot;\n&quot;
             incr x
       }
}
  
          
proc mainloop {{flg &quot;0&quot;}} {
global interval argv filelist1 filelist2

          if {$flg < 1} {
            set filelist2 {}
            openDir [lindex $argv 0] filelist1
            #lprint &quot;In main at $flg loop filelisting: &quot; $filelist1
            #after 5000 {clrscrn 25}
            after $interval {mainloop 1}
          } else {
            openDir [lindex $argv 0] filelist2
            #lprint &quot;In main at $flg loop, comp listing =&quot; $filelist2
            #after 5000 {clrscrn 25}
            set pnew [compFiles]
                  if {[llength $pnew]} {action &quot;less&quot; $pnew}
            set filelist1 {}
            set filelist2 {}
            after $interval {mainloop 0}
          }
} 
             
             
#main

      if {[set myid [fork]] == 0} {
           puts &quot;Daemonized at [pid]&quot;        
           mainloop
           vwait forever
      } else {
           #end parent and kill for errors
           exit
      }

Call like:
tclsh scriptname &quot;/dir&quot;
 
Here's my pretty simple code :)
Code:
#!/bin/sh

# To run this program every 15 minutes....
# put this line in your crontab file (type 'man crontab' for info
# 0,15,30,45 * * * * /path/to/this/program

cd .                            # or wherever the files are

# make sure the marker file exists
# this only happens once, just in case you forget
if [ ! -f marker ]; then touch marker; fi

# find all files which are named &quot;f*&quot; (whatever matches the files)
# which are files (-type f)
# and are newer than the marker (-newer marker)
newfile=`find . -name &quot;f*&quot; -type f -newer marker -print`
if [ &quot;$newfile&quot; != &quot;&quot; ];        # found something
then
  echo $newfile                 # replace echo with your own program
  touch -r $newfile marker      # make the marker the same age as the file
fi
Given that your files arrive about every 30 minutes, looking every 15 should be plenty to catch the arrival.

Beware - this expects at most ONE file at present, so if there is an unexpected flood of files, something else will be needed.

 
Greetings,
I have a couple of options in the FAQ for this forum under &quot;How do I monitor for a new file is in a directory&quot;.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top