處理nohup命令生成的大檔案
工作的時候,用到了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