我有cronjob每天在特定时间内运行脚本。该脚本用于转换特定文件夹中的大文件(约2GB)。问题是,并非每天我的coleague在时间之前将文件放在文件夹中,而是写为cronjob。
请帮助我在脚本中添加命令或编写第二个脚本:
- 检查文件是否存在于文件夹中。
- 如果以前的操作是正确的,请每分钟检查文件大小。(我想避免转换仍然不在大型文件)。
- 如果Filesize保持不变2分钟,请启动脚本进行转换。
我到目前为止给您脚本的重要行:
cd /path-to-folder
for $i in *.mpg; do avconv -i "$i" "out-$i.mp4" ; done
10倍帮助!
评论后新代码:
文件夹中有文件!
#! /bin/bash
cdate=$(date +%Y%m%d)
dump="/path/folder1"
base=$(ls "$dump")
if [ -n "$file"]
then
file="$dump/$base"
size=$(stat -c '%s' "$file")
count=0
while sleep 10
do
size0=$(stat -c '%s' "$file")
if [ $size=$size0 ]
then $((count++))
count=0
fi
if [ $count = 2 ]
then break
fi
done
# file has been stable for two minutes. Start conversion.
CONVERSION CODE
fi
终端中的消息:也许是错误???
script.sh: 17: script.sh: arithmetic expression: expecting primary: "count++"
file=/work/daily/dump/name_of_dump_file
if [ -f "$file" ]
then
# size=$(ls -l "$file" | awk '{print $5}')
size=$(stat -c '%s' "$file")
count=0
while sleep 60
do
size0=$(stat -c '%s' "$file")
if [ $size = $size0 ]
then : $((count++))
else size=$size0
count=0
fi
if [ $count = 2 ]
then break
fi
done
# File has been stable for 2 minutes — start conversion
fi
给定稍微修订的要求(在评论中描述),并假设文件名不包含空格,新线或其他类似尴尬的字符,则可以使用:
dump="/work/daily/dump" # folder 1
base=$(ls "$dump")
if [ -n "$file" ]
then
file="$dump/$base"
...code as before...
# File has been stable for 2 minutes - start conversion
dir2="/work/daily/conversion" # folder 2
file2="$dir2/$(basename $base .mpg).xyz"
convert -i "$file" -o "$file2"
mv "$file" "/work/daily/originals" # folder 3
ncftpput other.coast.example.com /work/daily/input "$file2"
mv "$file2" "/work/daily/converted" # folder 4
fi
如果文件夹中没有任何内容,则该过程将退出。如果要等到要转换文件,则需要围绕文件测试循环:
while file=$(ls "$dump")
[ -z "$file" ]
do sleep 60
done
这使用了壳循环的鲜为人知的功能;您可以将命令堆叠在控件中,但它是控制循环的最后一个状态。
好吧,我终于制作了一些工作代码:
#!/bin/bash
cdate=$(date +%Y%m%d)
folder1="/path-to-folder1"
cd $folder1
while file=$(ls "$folder1")
[ -z "$file" ]
do sleep 5 && echo "There in no file in the folder at $cdate."
done
echo "There is a file in folder at $cdate"
size1=$(stat -c '%s' "$file")
echo "The size1 is $size1 at $cdate"
size2=$(stat -c '%s' "$file")
echo "The size2 is $size2 at $cdate"
if [ $size1 = $size2 ]
then
echo "file is stable at $cdate. Do conversion."
是下一行循环相同脚本的正确行?
else sh /home/user/bin/exist-stable.sh
fi
以下评论后的正确代码为
else exec /home/user/bin/exist-stable.sh
fi