Shell脚本文件观察者并发

时间:2016-09-12 09:57:06

标签: linux bash shell unix

我有以下脚本(在OEL5.6上运行的shell脚本),当前通过cron安排从给定目录中选择文件(在数据库表中指定)并在目录上调用它们上的处理脚本&文件掩码基础。该脚本目前工作正常,但是如果一个文件夹有大量要处理的文件,即使所有其他文件夹都完成,脚本也不会退出,直到该文件夹​​在其他文件夹中登陆,被拿起直到下一次运行。我想对此使用类似的方法,但要让它不断检查文件夹中的新文件,而不是依次运行所有文件夹一次然后退出,这样它就会在后台不断运行更多的守护进程。任何想法,而不是在while true循环中包装它?我已经过滤掉了这个例子中的一些代码以保持简短。

readonly HOME_DIR="$(cd $(dirname $0)/;echo $PWD)"
export LOCK_DIR="/tmp/lock_folder"

check_lock() {
    # Try and create the $LOCK_DIR lock directory. Exit script if failure.
    # Do some checks to make sure the script is actually running and hasn't just failed and left a lock dir.
}

main(){
    # Check to see if there's already an instance of the watcher running.
    check_lock

    # when the watcher script exits remove the lock directory for the next run
    trap 'rm -r $LOCK_DIR;' EXIT

    # Pull folder and file details into a csv file from the database -> $FEEDS_FILE

    # Loop through all the files in given folders
    while IFS="," read feed_name feed_directory file_mask
    do

        # Count the number of files to process using the  directory and file mask
        num_files=$(find $feed_directory/$file_mask -mmin +5 -type f 2> /dev/null | wc -l 2> /dev/null)
        if [[ $num_files < 1 ]]; then
            # There's no files older than 5 mins to pickup here. Move on to next folder.
            continue 
        fi

        # Files found! Try and create a new feed_name lock dir. This should always pass first loop. 
        if mkdir $LOCK_DIR/$feed_name 2>/dev/null; then 
            $HOME_DIR/another_script.sh "$feed_name" "$feed_directory" "$file_mask" & # Call some script to do processing. This script removes it's child lock dir when done. 
        else
            log.sh "Watcher still running" f
            continue
        fi

        # If the amount of processes running as indicated by child lock dirs present in $LOCK_DIR is greater than or equal to the max allowed then wait before re-trying another.
        while [ $(find $LOCK_DIR -maxdepth 1 -type d -not -path $LOCK_DIR | wc -l) -ge 5 ]; do
            sleep 10
        done

    done < $FEEDS_FILE

    # Now all folders have been processed make sure that this script doesn't exit until all child scripts have completed (and removed their lock dirs).
    while [ $(find $LOCK_DIR -type d | wc -l) -gt 1 ]; do
        sleep 10
    done

    exit 0
}

main "$@"

1 个答案:

答案 0 :(得分:2)

一个想法是使用inotifywait中的inotify-tools来监控目录的变化。这比不断扫描目录以进行更改更有效。像这样的东西

inotifywait -m -r -e create,modify,move,delete /dir1 /dir2 |
    while IFS= read -r event; do
        # parse $event, act accordingly
    done