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
recursive chmod that only affects directories? retrovertigo UNIX for Advanced & Expert Users 1 06-22-2007 04:02 PM
Recursive pid script jbarnhar Shell Programming and Scripting 3 05-02-2007 03:14 PM
Problem with Recursive function malle Shell Programming and Scripting 4 02-03-2007 10:40 AM
directories problem yeah016 UNIX for Dummies Questions & Answers 2 07-22-2006 07:48 AM
grep recursive directories jagannatha UNIX for Dummies Questions & Answers 8 07-24-2003 04:00 PM

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

Join Date: Feb 2008
Posts: 6
Script problem due to recursive directories Help please

Hello everyone , I am looking for a better solution then the one I have created for the my Task

The task is:
Create an automated script that will check for Uploads in a specified Directory and move them to another specified Directory if the files are completely uploaded.

Files are FTP'd to the Specified directory
Multiple FTPS could be running so knowing if a particular FTP is complete is difficult for me to check by Process ID


I created the script listed below , but can see that issues can arise with Uploads of Trees
for instance
If the Upload directory has the following:

Quote:
# ls -ltR UPLOAD_DIR/
UPLOAD_DIR/:
total 4
drwxr-xr-x 4 root root 4096 Apr 26 21:32 Directory1
-rw-r--r-- 1 root root 0 Apr 26 21:31 file1.txt

UPLOAD_DIR/Directory1:
total 8
drwxr-xr-x 2 root root 4096 Apr 26 21:33 SubDir2
drwxr-xr-x 2 root root 4096 Apr 26 21:32 SubDir1
-rw-r--r-- 1 root root 0 Apr 26 21:32 dir1file1.txt

UPLOAD_DIR/Directory1/SubDir2:
total 0
-rw-r--r-- 1 root root 0 Apr 26 21:33 subdir2file2.txt
-rw-r--r-- 1 root root 0 Apr 26 21:33 subdir2file1.txt

UPLOAD_DIR/Directory1/SubDir1:
total 0
-rw-r--r-- 1 root root 0 Apr 26 21:32 subdir1file2.txt
-rw-r--r-- 1 root root 0 Apr 26 21:32 subdir1file1.txt
I am not sure , but if the last changed date of the stat command on a directory is older then the Age I am looking for
then shouldn;t that mean
that any file and sub directories below that Directory will be completely uploaded and also older then the Age I am looking for ?
...... I hope that made sense....
I do not think that is the case though....

I am hoping there is a better way to deal with this issue.

Thanks for being there.

Code:
#!/bin/bash
#
#
# This script will be used to monitor uploads to a specified directory
# and move them to another directory for downloading.
#
# Things to consider:
#       1. Was the uploaded files uploaded in a sub directory.
#       2. Are the files being uploaded complete.
#       3. Move the files to the specified directory
#          with the proper permissions.
#
#
#-------------------------------------------------------------------------------#
#
# Functions
#
#-------------------------------------------------------------------------------#
calc_time() {
        AGE_LAST_CHANGED=`expr ${CURRTIME} - ${FILE_LAST_CHANGED}`
        if [ ${AGE_LAST_CHANGED} -lt ${ACCEPTED_AGE} ];
        then
                OK_TO_MOVE=N
        else
                OK_TO_MOVE=Y
        fi
}
check_date() {
        FILE_LAST_CHANGED=`stat -t ${NEWFILE} |awk '{print $13}'`
}
#-------------------------------------------------------------------------------#
#
# Vairables will go here
#
#-------------------------------------------------------------------------------#
UPUSER=up_user
DOWNUSER=down_user
#
UPPATH=/home/Private/Uploads
DOWNPATH=/home/Private/Downloads

CURRTIME=`date +%s`
ACCEPTED_AGE=300        # 5 minutes of no activity according to date modified


for f in $( ls ${UPPATH} ); do

if [ -d ${UPPATH}/${f} ];
then
#--- Directory with Subfiles In it ---#
        DIR=${UPPATH}/${f}
        echo "${DIR} is a Directory"
        DIRDATE=`date +%Y_%m_%d_%H%M%S`
        NEWDIR=${DOWNPATH}/${f}_${DIRDATE}
        NEWFILE=${DIR}
        check_date
        calc_time
                # It appears that the directory has not been changed in the allowed timeframe (ACCEPTED_AGE)
                # it is ok to create a new directory in the DOWNPATH now.
                # set permissions of new directory to the download user
        mkdir ${NEWDIR}

        # This causes a problem by leaving the original Directory present in th UPLOADS directory.

        for f2 in $(ls  ${UPPATH}/$f ); do
                NEWFILE=${DIR}/${f2} # Set becuase of check_date function
                check_date
                calc_time
                if [ ${OK_TO_MOVE} = "Y" ];
                then
                        mv ${NEWFILE} ${NEWDIR}
                        echo "mv ${NEWFILE} ${NEWDIR} "
                else
                        echo "File date is too new to move better wait"
                fi

        chown -R ${DOWNUSER}:${DOWNUSER} ${NEWDIR}
        done

else
#--- File is in Main Upload directory ---#
        if [ -f ${UPPATH}/$f ]; then
                NEWFILE=${UPPATH}/${f}
                echo "${NEWFILE} is a file"
                check_date
                calc_time

                if [ ${OK_TO_MOVE} = "Y" ];
                then
                        mv ${NEWFILE} ${DOWNPATH}
                        echo "mv ${NEWFILE} ${DOWNPATH} "
                        chown ${DOWNUSER}:${DOWNUSER} ${DOWNPATH}/*
                else
                        echo "File date is too new to move better wait"
                        echo "next pass of cronjob should move this file"
                fi
        fi
fi

done
  #2 (permalink)  
Old 04-27-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
Hello... I would tackle this problem in PERL, but for now I wanted to address options for knowing if the file upload is complete... The usual way to do this is to have the person or software that does the uploading to RENAME (ftp REN command) to another directory in the same file system. Because a RENAME (like a mv move in the shell) only changes pointers, the process is instantaneous and so if the file appears in the moved-to directory, you know it must be complete....

so for example...

../uploaddir/commitdir

You UPLOAD files to the uploaddir and when the upload is finished issue a command like ftp> REN uploadedfile commitdir/uploadedfile

Your process polls commitdir, not the uploaddir. If a file appears in commitdir, you know it must be fully uploaded and closed.
  #3 (permalink)  
Old 04-27-2008
RobertCo RobertCo is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 1
Hello quine, thanks for the response,
The problem I have is that I want to have a cron job run this script
and the directory that the files are moved to are not visible nor accessible by the uploader. Once the files are uploaded and moved, the uploader no longer has access to them. (well accept for the small timeframe between cron jobs approx. 1-5 minutes max)

A perl solution would be acceptable to me, accept I have coded only one module in perl in my life , You probably heard of it..... "Hello World"


Shell scripting I can muddle through as you can see by the script I have written so far.

So any example even in perl would benefit my issue.
thanks again
Sponsored Links
Closed Thread

Bookmarks

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 04:41 PM.


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