linux 内存溢出检查,linux - 远程检查Linux中是否存在所有文件? - 堆栈内存溢出...

我有一个下面的脚本,该脚本迭代所有计算机并远程检入每台计算机:

是否存在特定目录?

并且该目录中是否存在所有2000个文件。

下面是代码:

for machine in "${MACHINES[@]}"; do

dircheck=($(ssh -o "StrictHostKeyChecking no" user@${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))

if [[ $? != 0 ]] ;then

echo "Folder $dir3 doesn't exist on $machine" >&2

exit 1

fi

# this is for checking all the 2000 files

# this check is very slow when it checks all 2000 files

for el in ${FILES[@]}

do

printf -v cmd '%q ' test -e "$dir3/process_${el}.log"

ssh user@${machine} "$cmd" \

|| { echo "File number $el missing on $machine." >&2;

exit 1; }

done

done

现在的问题是检查所有2000个文件需要花费很多时间,所以想看看是否有什么方法可以使我们做同样的事情,但是速度很快?

更新:

因此,总体而言,我的脚本将如下所示:

readonly MACHINES=(machineA machineB machineC)

readonly dir3=/some_path

echo $dir3

FILES=({0..1999})

checkFunc() {

test -d "$dir3" || echo "NODIR"

local filename successCount=0

while IFS= read -r filename; do

test -e "$dir3/process_${filename}.log" && (( ++successCount ))

done

printf '%s\n' "$successCount"

}

for machine in "${MACHINES[@]}"; do

actual=$(

printf '%s\0' "${FILES[@]}" | \

ssh "$machine" "$(declare -p dir3; declare -f checkFunc); checkFunc"

) || { echo "ERROR: Unable to retrieve remote file count" >&2; exit 1; }

case $actual in

(${#FILES[@]}) echo "SUCCESS: Expected, and found, $numberOfActuallyRemoteFiles files" ;;

(NODIR) echo "FAILURE: Directory $dir3 does not exist" ;;

(*) echo "FAILURE: Out of ${#FILES[@]} files, only $actual exist" ;;

esac

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值