linux logrotate日志轮询设置案例一

本文介绍了如何使用Linux的logrotate工具进行日志轮换,包括编辑`/etc/logrotate.conf`文件,设置每小时轮换一次,创建新日志文件的权限,以及配置轮换后执行的脚本。此外,还涉及到将cron任务从daily改为hourly,以及重启crond服务来确保设置生效。
摘要由CSDN通过智能技术生成

1.编辑/etc/logrotate.conf文件,添加如下配置,并保存

/var/log/ztj.log {
    missingok
    hourly
    create 644 root root
    sharedscripts
    postrotate
    if [ -f /var/run/syslogd.pid ];then
    /bin/kill -HUP $(/bin/cat /var/run/syslogd.pid) >/dev/null 2>&1 || true
    fi
    endscript
}

/var/log/ztj.log {  #转储日志路径
    missingok      #当日志文件不存在时,忽略该日志文件的警告信息
    hourly            #每小时转储一次
    create 644 root root   #指定转储后创建新文件,权限为644,用户为root,属组为root
    sharedscripts   #该关键字后的脚本只执行一次
    postrotate     #在日志轮替之后执行脚本命令
    if [ -f /var/run/syslogd.pid ];then
    /bin/kill -HUP $(/bin/cat /var/run/syslogd.pid) >/dev/null 2>&1 || true   #重启syslogd应用
    fi
    endscript        #在日志轮替之后执行脚本命令
}

2.拷贝/etc/cron.daily/logrotate文件至/etc/cron.hourly,权限要保持一致

命令:

\cp -arpf /etc/cron.daily/logrotate /etc/cron.hourly/

[root@rhel77 cron.daily]# ls -l /etc/cron.daily/logrotate 
-rwx------. 1 root root 219 Jun 15  2018 /etc/cron.daily/logrotate
[root@rhel77 cron.daily]# ls -l /etc/cron.hourly/logrotate 
-rwx------. 1 root root 219 Jun 15  2018 /etc/cron.hourly/logrotate
[root@rhel77 cron.daily]# 

3.重启crond

命令:

systemctl restart crond

[root@rhel77 cron.daily]# systemctl restart crond
[root@rhel77 cron.daily]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2023-11-16 09:50:45 CST; 3s ago
 Main PID: 3336 (crond)
    Tasks: 1
   CGroup: /system.slice/crond.service
           └─3336 /usr/sbin/crond -n

Nov 16 09:50:45 rhel77 systemd[1]: Started Command Scheduler.
Nov 16 09:50:45 rhel77 crond[3336]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 39% if used.)
Nov 16 09:50:45 rhel77 crond[3336]: (CRON) INFO (running with inotify support)
Nov 16 09:50:45 rhel77 crond[3336]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
[root@rhel77 cron.daily]# 

4.验证

[root@rhel77 log]# ls -l | grep ztj
-rw-r--r--  1 root   root       1637 Nov 16 10:07 ztj.log
-rw-r--r--  1 root   root      38301 Nov 16 09:03 ztj.log-2023111609
-rw-r--r--  1 root   root       3600 Nov 16 10:01 ztj.log-2023111610
[root@rhel77 log]# 
  • 31
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值