The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
server monitor script... zedex Shell Programming and Scripting 1 06-01-2008 04:10 PM
need help doing a script to monitor if files are go through jonathan184 Shell Programming and Scripting 0 05-15-2007 11:47 AM
load monitor script locabuilt UNIX for Advanced & Expert Users 7 01-19-2007 02:37 PM
Hep with script to monitor directory cmf00186 UNIX for Dummies Questions & Answers 2 10-25-2006 02:42 PM
Monitor which users enter my home directory mnpradeep High Level Programming 1 03-21-2002 05:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-20-2008
nulinux nulinux is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
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 11:20 AM..
  #2 (permalink)  
Old 08-20-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
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 11:55 AM.. Reason: sequence fix
  #3 (permalink)  
Old 08-20-2008
nulinux nulinux is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
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 (permalink)  
Old 08-20-2008
nulinux nulinux is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
Quote:
Originally Posted by otheus View Post
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 (permalink)  
Old 08-20-2008
nulinux nulinux is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
what about using the watch command?

thanks
  #6 (permalink)  
Old 08-21-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Quote:
Originally Posted by nulinux View Post
what about using the watch command?
Useful for "eye-balling" the status of a process or whatever. But: No way to trigger a command (automatically) upon a condition, and Do you really want to sit there and watch for files to come in? If you do, then :

Code:
watch ls -l $MAILBOX
is fine.

Quote:
also in your while statement, when would the condition not be true?
Never. You'd have to kill the process. It's equivalent to removing the while loop and putting the script as a cron job that runs ever 5 minutes.

Quote:
What is your if statement actually doing?
It's running the pipeline of ls and grep. The grep is looking for any line. If ls finds no files, it outputs no line, and the grep search fails. If the grep search fails, it exits with 1 which (in BASH logic) means false, and so the if condition fails. If the grep finds at least one line, this means there are files and so the if condition succeeds and the THEN portion is executed.

Quote:
Originally Posted by nulinux View Post
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.
Are there other files in the directory besides these?

Quote:
Originally Posted by nulinux View Post
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?
There are at least four solutions to this. In the worst case, you can use what is suggested by ddreggors. Here are solutions to try:
  1. Move-after-write. Modify (or configure) the process that places the incoming file. When creating the file, it names the file in a distinctive way (different extension, prefixed with ., different path, etc). After closing, it moves / renames the file in a way your script expects.
  2. Keep control-log file. Modify (or configure) the process that places the incoming file. After closing the file, it appends the name of the file to a control file, kept inside the mailbox folder (as "something.ctl"). Your script will rename this file, and then read it for a list of file names to sftp.
  3. Use flock. This will only work IF the process, which places the incoming files, uses flock(2) on files that it creates. Use the script to use flock (shell command) to each file before it is moved.

ddreggors solution is a bit resource-intensive, and VERY linux-specific, but it will work if the uploading process does not close/reopen the file in between writes AND if it removes the file after failure (if the upload process is interrupted and the file is not completely transferred). It will fail if there are any other benign processes reading the incoming files. Here is a re-write of that solution which is more efficient (ie, doesn't use additional forks):

Code:
cd $MAILBOX
LSOF=/usr/sbin/lsof
for f in *.dat; do
  if  $LSOF $MAILBOX/$f ; then
     # trigger action goes here.
     sftp $MAILBOX/$f someone@somewhere:destination/$f
  fi
done

Last edited by otheus; 08-21-2008 at 04:01 AM.. Reason: formatting fix
  #7 (permalink)  
Old 08-21-2008
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
Quote:
Originally Posted by otheus View Post
it will work if the uploading process does not close/reopen the file in between writes AND if it removes the file after failure (if the upload process is interrupted and the file is not completely transferred).
Nice rewrite and great points but I thought I might mention that in the quote above you make it seem like it will NOT work if "the upload process is interrupted and the file is not completely transferred". While that is not what one would want the process will still work, you will just end up with a blank or partially written file. What I mean to say is that the loop will still run and do what it is meant to do but it will preform the actions on a file that is not intact. This would happen with ANY process as far as I know. If the transfer fails then you have a bad file and no way to tell without looking inside manually (vi, nano, etc...) or diff against the original (which you might not have).

Now on the other hand, it would be a very nice feature if the upload/transfer process failed and the file was removed (as you already mentioned).

As to the Linux specific code... my bad, I am from a Linux world and should remember that nix* means *more* than Linux usually.

I also make the assumtion that this user wants a quick easy to create solution. I base this on very little knowledge granted but there seems to be an air of "quick dirty hack" (not to be taken as a bad thing, just a real world thing) written in the initial question.
Closed Thread

Bookmarks

Tags
sftp script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0