Quote:
Originally Posted by nulinux
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
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
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:
- 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.
- 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.
- 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