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

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,测试效果:

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 //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.