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]#