我有一個是通過cron目前預定以下腳本(shell腳本在運行OEL5.6)來接從給定的目錄中的文件了(在數據庫表中指定),並呼籲他們處理腳本在目錄&文件掩碼的基礎上。該腳本能正常工作的時刻,但這個實現,如果一個文件夾中有大量文件的處理,即使所有其他文件夾完成腳本將不會退出,直到一個人,這意味着文件降落在其他文件夾不會被拿起,直到下一次運行。我想使用類似的方法,但它不斷地檢查文件夾中的新文件,而不是按順序遍歷所有文件夾一次,然後退出,因此它會在後臺不斷運行更多的守護進程。任何想法,而不是將其包裝在while true
循環中?我已經從這個例子中濾除了一些代碼,以保持簡短。shell腳本文件守望者併發
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 "[email protected]"
什麼操作系統是什麼? – redneb
操作系統是OEL 5.6 –