1. 程式人生 > 其它 >centos8(linux): nohup生成的日誌切分

centos8(linux): nohup生成的日誌切分

一,bash程式碼

1,splitlog.sh
[lhdop@blog tools]$ more splitlog.sh
#!/bin/bash
LOG_PATH=/data/store/back/logs/back.log
now_date=`date +%Y%m%d`
DEST_PATH=/data/store/back/logs/back${now_date}.log
echo "dest log path:$DEST_PATH";
cp -axv $LOG_PATH $DEST_PATH
cat /dev/null > $LOG_PATH
2,放到crontab中定時執行:
 [lhdop@blog tools]$ crontab -l
55 23 * * * /data/store/back/tools/splitlog.sh

說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest

對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/
或:https://gitee.com/liuhongdi

說明:作者:劉巨集締 郵箱: [email protected]

二,測試效果:

1,檢視被切分的檔案:
[lhdop@blog logs]$ ll
total 16272-rw-rw-r-- 1 lhdop lhdop 15392197
Feb 11 16:16 back202202.log
2,執行:
[lhdop@blog tools]$ ./splitlog.sh
dest log path:/data/store/back/logs/back20220211.log
lines:169360
'/data/store/back/logs/back202202.log' -> '/data/store/back/logs/back20220211.log’
3,檢視效果:
[lhdop@blog tools]$ ll ../logs
total 16272-rw-rw-r-- 1 lhdop lhdop 15392197 Feb 11 16:16
back20220211.log -rw-rw-r-- 1 lhdop lhdop 0 Feb 11 17:18 back202202.log

三,檢視bash和linux的版本:

linux

[lhdop@blog tools]$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
 
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION=“8"

bash

[lhdop@blog tools]$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.