script to monitor directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to monitor directory
# 1  
Old 08-20-2008
script to monitor directory

What is the best way for a script to run to monitor a directory for the presence of files and then perform a function afterwords? I was hoping to have it continually run and sleep until it detects that files are present in the directory, then break out of the loop and go on to the next step.

What it does it waits for files to be transferred to it's local directory, then when the files are present it sftp's them out to another host. I already have the sftp script working just need the proper syntax for a loop to go to sleep until it detects the files in the directory. Also the file names and conventions will constantly change so I was hoping more for a 0 byte count compared to anything else besides a 0 byte count for a condition.This is a linux box using bash as the shell.

thanks in advance guys.

Last edited by nulinux; 08-20-2008 at 12:20 PM..
# 2  
Old 08-20-2008
Classic mailbox problem. One problem you have is: how do you know the files are really there, as opposed to partly there? Also, after you sftp them out, do you remove them?

Without addressing these problems (above), here's a simple script:
Code:
MAILBOX=some_directory
seq=0
while true; do 
   let seq=seq+1   # this might be bash specific
   if  /bin/ls -1 $MAILBOX | grep ^ ; then
      # make sure incoming files aren't mixed up with files-to-be-processed
      mv $MAILBOX $MAILBOX.$seq
      mkdir $MAILBOX
      # your function
      sftp_this_directory $MAILBOX.$seq
      # clean up 
      rm -rf $MAILBOX.$seq
   fi

   sleep 300 # wait 5 minutes
done


Last edited by otheus; 08-20-2008 at 12:55 PM.. Reason: sequence fix
# 3  
Old 08-20-2008
One problem you have is: how do you know the files are really there, as opposed to partly there? Also, after you sftp them out, do you remove them?

Hello, thanks for the quick response! You have good points and this has been discussed. We do remove them after transfer. another note is that although the names will change as will the byte size, they will always end in *.dat. One of other problems is as you mention what if the files are detected before they are finished transferring to the host, before they are sent back out?

thanks
# 4  
Old 08-20-2008
Quote:
Originally Posted by otheus
Classic mailbox problem. One problem you have is: how do you know the files are really there, as opposed to partly there? Also, after you sftp them out, do you remove them?

Without addressing these problems (above), here's a simple script:
Code:
MAILBOX=some_directory
seq=0
while true; do 
   let seq=seq+1   # this might be bash specific
   if  /bin/ls -1 $MAILBOX | grep ^ ; then
      # make sure incoming files aren't mixed up with files-to-be-processed
      mv $MAILBOX $MAILBOX.$seq
      mkdir $MAILBOX
      # your function
      sftp_this_directory $MAILBOX.$seq
      # clean up 
      rm -rf $MAILBOX.$seq
   fi

   sleep 300 # wait 5 minutes
done



What is your if statement actually doing?

thanks
# 5  
Old 08-20-2008
what about using the watch command?

thanks
# 6  
Old 08-20-2008
also in your while statement, when would the condition not be true?
thanks
# 7  
Old 08-20-2008
better yet create a script that runs "one" time to look for (ls) files and use lsof to add logic for files currently being written to, next add a function for what you want to do with files NOT being written to.

Place that script in a users (root?) cron that runs every minute.

example of logic using lsof:

for F in `ls`
do
VAL=`lsof /full/path/to/$F`
if [ -z $VAL ]
then
your_function_here $F
fi
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script monitor directory and subdirectories for new pdfs

I need bash script that monitor folders for new pdf files and create xml file for rss feed with newest files on the list. I have some script, but it reports errors. #!/bin/bash SYSDIR="/var/www/html/Intranet" HTTPLINK="http://TYPE.IP.ADDRESS.HERE/pdfs" FEEDTITLE="Najnoviji dokumenti na... (20 Replies)
Discussion started by: markus1981
20 Replies

2. Shell Programming and Scripting

awk to monitor size in directory

i'm trying to find the most efficient way to monitor specific files in different directories in one go. /var/log/ /var/app/ /var/db/ each one of these directories can have subdirectories which need to be looked into as well. I want to find any file in this directory that has the name... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

4. Shell Programming and Scripting

Script to monitor directory size of specific users

Hi, i am new to shell scripts, i need to write a script that can monitor size of directory of specific users. Please help. Thanks, Nitin (2 Replies)
Discussion started by: nicksrulz
2 Replies

5. Shell Programming and Scripting

Directory Monitor in KSh

Hi, I need to write a directory monitor, i.e. a korn shell script which would Report changes to the directory contents, like: added file1, deleted file2, updated file3 , created subdir (optional)... There is no specific file pattern. So far I have written a little script... (1 Reply)
Discussion started by: olegkon
1 Replies

6. UNIX for Dummies Questions & Answers

Monitor directory and email

Hello all, Can anyone please guide / help me in the following task.... I have a directory where some external users will upload pdf files. The filename of these pdf will be of a particular format (<id>-<first name>_<last name>_<some number>.pdf) I want to make a script such that it takes... (6 Replies)
Discussion started by: dhawalkv
6 Replies

7. AIX

monitor directory events

I'm am looking for a cheap way to trigger a script when a new file is written in a specific directory. AIX 5.3. It is a production system, so no kernel patching (i.e. inotify). Filemon and audtiing are too expensive. Thanks in advance. (2 Replies)
Discussion started by: pbillast
2 Replies

8. Shell Programming and Scripting

Monitor capacity of directory

Good morning. I have been attempting to find a way to monitor the capacity of a directory so that when it reaches 80% or higher I can send an event. I was able to find a script that does this for the whole drive by I can not seem to figure out how to do this for just a single directory. ... (1 Reply)
Discussion started by: LRoberts
1 Replies

9. Shell Programming and Scripting

script to monitor files in a directory and sending the alert

Hi All, We are having important config files in an directory which was accessable by all /auto/config/Testbed/>ls config1.intial config2.intial config3.inital often we find that some of the lines are missing in config files, we doubt if some one is removing. I would like to write... (0 Replies)
Discussion started by: shellscripter
0 Replies

10. UNIX for Dummies Questions & Answers

Hep with script to monitor directory

Hello, I am a newbie who is attempting to write a script to monitor a directory for a set of 3 files that I am expecting to get ftp'd. Occasionally, we suspend operations for maintenance etc. but we still get the files so there can be more than 1 set. If there is more than 1 set, I would like... (2 Replies)
Discussion started by: cmf00186
2 Replies
Login or Register to Ask a Question