美文网首页Go
处理nohup命令生成的大文件

处理nohup命令生成的大文件

作者: 钱铜 | 来源:发表于2018-10-10 00:02 被阅读13次

    工作的时候,用到了Golang开发项目,将Golang打包(go build)生成执行文件,之后用nohup命令运行

    nohup ./gopack &
    

    假设gopack是生成的执行文件,上面的命令执行后,要在当前目录下生成一个nohup.out文件,在不停下进程的情况下,nohup.out文件会越来越大,于是就打算对它切分另存并清空。

    #!/bin/bash
    thepath=/data/testshell
    
    if [ ! -d "$thepath/log/" ];then
    mkdir $thepath/log
    fi
    
    chmod -R 777 $thepath/log
    
    
    if [ -f "$thepath/nohup.out" ];then
    
    cpDate=`date -d "-1 day" "+%Y%m%d"`
    cp $thepath/nohup.out $thepath/log/log_${cpDate}.log
    cat /dev/null > $thepath/nohup.out
    split -b 512 -d -a 4 $thepath/log/log_${cpDate}.log $thepath/log/log_${cpDate}_
    rm -fr $thepath/log/log_${cpDate}.log
    
    fi
    

    下面两句是先把nohup.out复制,然后清空:

    cp $thepath/nohup.out $thepath/log/log_${cpDate}.log
    cat /dev/null > $thepath/nohup.out
    

    split命令对另存的文件切分

    split -b 512 -d -a 4 $thepath/log/log_${cpDate}.log $thepath/log/log_${cpDate}_
    

    最后删除另存的文件:

    rm -fr $thepath/log/log_${cpDate}.log
    

    相关文章

      网友评论

        本文标题:处理nohup命令生成的大文件

        本文链接:https://www.haomeiwen.com/subject/qgsuaftx.html