V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DongDongXie
V2EX  ›  程序员

多条指令后台执行怎么同时写一个数组

  •  
  •   DongDongXie · 2019-09-01 17:04:46 +08:00 · 1401 次点击
    这是一个创建于 1698 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景

    有一堆文件,一些是 txt 文件,一些是 tag 文件,tag 文件里面放的是对应 txt 文件的 MD5 值,现在我需要校验 txt 文件的 MD5 值和 tag 文件里面记载的是否一致,文件比较多,所以我让校验那块儿逻辑在后台执行,如果一个文件校验没通过,我就把该文件名追加到一个名为 errfile 的文件中. 如果我不把文件名保存在文件中,要保存在一个数组里面,后台多个同时执行的任务就会同时写一个数组,这种有没有安全一点的处理办法呢

        for file in `hadoop fs -ls $tmptxtDir|awk '{print $8}'|awk -F'/' '{print $NF}'`
        do
            {
            tmpmd5=`hadoop fs -md5sum $tmptxtDir/$file`
            md5=`hadoop fs -cat $tmpmd5Dir/$file.tag`
            if [ "$tmpmd5" == "$md5" ];then
                hadoop fs -cp $tmptxtDir/$file $finaltxtdataDir/$timestamp
                hadoop fs -cp $tmpmd5Dir/$file.tag $finaltxtdataDir/$timestamp
            else
                echo $file >>errFile.txt
                echo "Failed now. $file::$tmpmd5::$md5"
            fi
            } &
            ((i++))
            echo $i
            val=$(( $i % 10 ))
            if [ $val -eq 0 ]; then wait; fi
        done
    
    3 条回复    2019-09-01 21:12:47 +08:00
    5thcat
        1
    5thcat  
       2019-09-01 19:22:16 +08:00   ❤️ 1
    [Terminal #1] $ lockfile -r 0 /tmp/the.lock
    [Terminal #1] $

    [Terminal #2] $ lockfile -r 0 /tmp/the.lock
    [Terminal #2] lockfile: Sorry, giving up on "/tmp/the.lock"

    [Terminal #1] $ rm -f /tmp/the.lock
    [Terminal #1] $

    [Terminal #2] $ lockfile -r 0 /tmp/the.lock
    [Terminal #2] $
    AX5N
        2
    AX5N  
       2019-09-01 19:56:43 +08:00
    用队列,处理一个就踢出一个
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5576 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 02:42 · PVG 10:42 · LAX 19:42 · JFK 22:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.