2009-11-20 36 views
4

我有以下情況:bash腳本來觀看一個文件夾

有已經安裝在Linux機器上的Windows文件夾。可能有多個文件夾(安裝前手) 在這個窗口安裝。我必須做一些事情(最好是一個腳本開始)觀看這些文件夾。

這些步驟如下: 觀看任何傳入文件(多個)。確保它們完全轉移。 將其移至另一個文件夾。 我沒有在Windows機器上的文件傳輸程序的任何控制。我相信這是一個安全的FTP。 所以我不能要求這個過程給我拖文件,以確保文件傳輸的完成。

我寫了一個bash腳本。我想知道這種方法的潛在缺陷。理由是, 有多個目錄像這樣運行此腳本的多張拷貝的可能性。

目前,可能有多達100個可能需要監視的目錄。

以下是腳本。我很抱歉在這裏粘貼很長的一個。請花些時間來評論它並 評論/批評它。 :-)

它需要3個參數,需要觀察的文件夾,文件必須被移動的文件夾, 和時間間隔,這在下面已經解釋。

對不起,對齊似乎存在問題。 Markdown似乎並不喜歡它。我試圖妥善組織它,但無法做到這一點。

Linux servername 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:27:17 EDT 2006 i686 i686 i386 GNU/Linux

#!/bin/bash 
log_this() 
{ 
    message="$1" 
    now=`date "+%D-%T"` 
    echo $$": "$now ": " $message 
} 
usage() 
{ 
    cat << EOF 
Usage: $0 <Directory to be watched> <Directory to transfer> <time interval> 
Time interval is the amount of time after which the modification time of a 
file will be monitored. 
EOF 
    `exit 1` 
} 

if [ $# -lt 2 ] 
then 
    usage 
fi 

WATCH_DIR=$1 
APP_DIR=$2 

if [ ! -d "$WATCH_DIR" ] 
then 
    log_this "FATAL: WATCH_DIR, $WATCH_DIR does not exist. Exiting" 
    exit 1 
fi 

if [ ! -d "$APP_DIR" ] 
then 
    log_this "APP_DIR: $APP_DIR does not exist. Exiting" 
    exit 1 
fi 


# This needs to be set after considering the rate of file transfer. 
# Represents the seconds elapsed after the last modification to the file. 
# If not supplied as parameter, defaults to 3. 

seconds_between_mods=$3 

if ! [[ "$seconds_between_mods" =~ ^[0-9]+$ ]]; then 
     if [ ${#seconds_between_mods} -eq 0 ]; then 
       log_this "No value supplied for elapse time. Defaulting to 3." 
       seconds_between_mods=3 
     else 
       log_this "Invalid value provided for elapse time" 
       exit 1 
     fi 
fi 

log_this "Start Monitor." 

while true 
do 
     ls -1 $WATCH_DIR | while read file_name 
     do 
      log_this "Start Monitoring for $file_name" 

      # Refer only the modification with reference to the mount folder. 
      # If there is a diff in time between servers, we are in trouble. 

      token_file=$WATCH_DIR/foo.$$ 
      current_time=`touch $token_file && stat -c "%Y" $token_file` 
      rm -f $token_file 2>/dev/null 

      log_this "Current Time: $current_time" 
      last_mod_time=`stat -c "%Y" $WATCH_DIR/$file_name` 

      elapsed_time=`expr $current_time - $last_mod_time` 
      log_this "Elapsed time ==> $elapsed_time" 

      if [ $elapsed_time -ge $seconds_between_mods ] 
      then 
        log_this "Moving $file_name to $APP_DIR" 

        # In case if there is no space left on the target mount, hide the  file 
        # in the mount itself and remove the incomplete file from APP_DIR. 
        mv $WATCH_DIR/$file_name $APP_DIR 
        if [ $? -ne 0 ] 
        then 
          log_this "FATAL: mv failed!! Hiding $file_name" 
          rm $APP_DIR/$file_name 
          mv $WATCH_DIR/$file_name $WATCH_DIR/.$file_name 
          log_this "Removed $APP_DIR/$file_name. Look for $WATCH_DIR/.$file_name and submit later." 
        fi 

        log_this "End Monitoring for $file_name" 
      else 
        log_this "$file_name: Transfer seems to be in progress" 
      fi 
    done 
    log_this "Nothing more to monitor." 
    echo 
    sleep 5 
done 
+0

[用於更改監控目錄]的可能重複(http://stackoverflow.com/questions/511463/monitor-directory-for-changes) –

回答

5

這不會持續任何時間。在製作過程中,您將遇到網絡問題和其他可能會在上傳目錄中留下部分文件的錯誤。我也不喜歡「預告片」文件的想法。通常的做法是以臨時名稱上傳文件,然後在上載完成後重命名它。

這樣,你只需要列出目錄,篩選出臨時名稱和,如果有什麼事,使用它。

如果你不能讓這種變化,然後問你的老闆來實現一些東西,可以導致任意數據損壞的書面許可。這有兩個目的:1)爲了讓他們明白,這是一個真正的問題,而不是一些你化妝和2),以保護自己,當它打破...因爲它會和你猜誰就會得到所有的怪?

1

說實話Python應用程序設置爲在啓動時運行將快速,高效地做到這一點。 Python具有令人驚歎的操作系統支持,並且相當完整

運行腳本都會很成功,但它會是麻煩的照顧和管理。我認爲你會將這些作爲頻繁的cron作業來運行?

+0

嗯。我沒有Python知識。你能指點我一個python包或模塊(我不知道術語),可以幫助做到這一點?謝謝。 – prabhu

+0

1.按照www.python.org上的python教程,2.查看os.path模塊。它有一個walk()方法,您可以使用它來檢查目錄樹。 –

+0

Pynotify是一個python庫,它將被通知目錄中的更改:http://pyinotify.sourceforge.net –

4

我相信一個更理智的方法是使用一個內核級的文件的通知項目。如inotify。獲取工具here

+0

inotify似乎是正確的方式。但在這一點上,我無法修補我們的內核來安裝它。 – prabhu

0

爲了讓你脫穎而出,我寫了一個小程序,它需要一個路徑並查看jpeg文件的二進制輸出。我從來沒有完成它,但它會讓你開始,並看到蟒蛇的結構,以及一些使用操作系統..

我不會花太多時間擔心我的代碼。

import time, os, sys 

#analyze() takes in a path and moves into the output_files folder, to then analyze files 

def analyze(path): 
    list_outputfiles = os.listdir(path + "/output_files") 
    print list_outputfiles 
    for i in range(len(list_outputfiles)): 
     #print list_outputfiles[i] 
     f = open(list_outputfiles[i], 'r') 
     f.readlines() 

#txtmaker reads the media file and writes its binary contents to a text file. 

def txtmaker(c_file): 
    print c_file 
    os.system("cat" + " " + c_file + ">" + " " + c_file +".txt") 
    os.system("mv *.txt output_files") 

#parser() takes in the inputed path, reads and lists all files, creates a directory, then calls txtmaker. 

def parser(path): 
    os.chdir(path) 
    os.mkdir(path + "/output_files", 0777) 
    list_files = os.listdir(path) 
    for i in range(len(list_files)): 
     if os.path.isdir(list_files[i]) == True: 
      print (list_files[i], "is a directory") 
     else: 
      txtmaker(list_files[i]) 
    analyze(path) 

def main(): 
    path = raw_input("Enter the full path to the media: ") 
    parser(path) 


if __name__ == '__main__': 

    main() 
1

incron是一個 「的inotify的cron」 系統。它由一個守護進程和一個表操作器組成。你可以像使用普通的cron一樣使用它。區別在於inotify cron處理文件系統事件而不是時間段。

2

首先確認inotify-tools已安裝。

然後使用它們像這樣:

logOfChanges="/tmp/changes.log.csv" # Set your file name here. 

# Lock and load 
inotifywait -mrcq $DIR > "$logOfChanges" & # monitor, recursively, output CSV, be quiet. 
IN_PID=$$ 

# Do your stuff here 
... 

# Kill and analyze 
kill $IN_PID 
cat "$logOfChanges" | while read entry; do 
    # Split your CSV, but beware that file names may contain spaces too. 
    # Just look up how to parse CSV with bash. :) 
    path=... 
    event=... 
    ... # Other stuff like time stamps 
    # Depending on the event… 
    case "$event" in 
    SOME_EVENT) myHandlingCode path ;; 
    ... 
    *) myDefaultHandlingCode path ;; 
done 

另外,在使用inotifywait代替--format-c將是一個想法。

只需man inotifywaitman inotifywatch瞭解更多信息。

相關問題