我有以下情况:
在Linux机器上挂载了一个Windows文件夹。这个Windows挂载中可能有多个文件夹(事先设置好)。我需要做一些事情(最好是从脚本开始)来监视这些文件夹。
以下是步骤: 观察任何传入的文件。确保它们被完全传输。 将其移动到另一个文件夹。 我无法控制Windows机器上的文件传输程序。我相信这是一个安全的FTP。因此,我不能要求该进程向我发送尾随文件以确保文件传输完成。
我编写了一个bash脚本。我想知道这种方法可能存在的潜在问题。原因是,可能会有多个副本运行于像这样的多个目录中。
目前,最多可能有100个目录需要进行监视。
以下是脚本。很抱歉在此粘贴了一个非常长的脚本。请花时间审查并发表评论/批评。 :-)
它需要3个参数:要监视的文件夹、要移动文件的文件夹和一个时间间隔,下面已经解释过了。
很抱歉似乎对齐存在问题。Markdown似乎不喜欢它。我试图正确组织它,但无法做到。
在Linux机器上挂载了一个Windows文件夹。这个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