Migster
Migster

Reputation: 153

How to daily rotate logs, with data from the current day only?

I rotate my nginx access log files with logrotate an have the following config:

/var/www/logs/*.log {
    daily
    missingok
    dateext
    dateformat _%Y-%m-%d
    dateyesterday
    rotate 90
    compress
    delaycompress
    compressext
    notifempty
    create 0640 www-data www-data
    sharedscripts
    postrotate
           [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript

}

When cron runs that script for example at 5am all logs between 0am and 5am in the file will be rotated. I want a log file for exactly one day from midnight to 11.59pm

Is there an opportunity to configure this?

Upvotes: 2

Views: 3656

Answers (2)

Raj Bangarwa
Raj Bangarwa

Reputation: 544

To rotate the log at 11.59pm configure the /etc/crontab file as follow :

59 23 * * * <path_to_logrotate_command> -f <path_to_logrotate_schedule_file> 
E.g. 59 23 * * * /usr/sbin/logrotate -f /home/foo/logrotate/rotate_nginx

Upvotes: 1

ivanleoncz
ivanleoncz

Reputation: 10015

Actually, it would be better to just change the schedule of cron.daily, to something like this:

$ grep daily /etc/crontab 
59  23  * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

Remembering that this will be valid to each script inside /etc/cron.daily:

$ ls /etc/cron.daily -l
total 48
-rwxr-xr-x 1 root root  376 nov 20  2017 apport
-rwxr-xr-x 1 root root 1478 abr 20  2018 apt-compat
-rwxr-xr-x 1 root root  355 dic 29  2017 bsdmainutils
-rwxr-xr-x 1 root root 1176 nov  2  2017 dpkg
-rwxr-xr-x 1 root root  372 ago 21  2017 logrotate
-rwxr-xr-x 1 root root 1065 abr  7  2018 man-db
-rwxr-xr-x 1 root root  539 jun 26  2018 mdadm
-rwxr-xr-x 1 root root  538 mar  1  2018 mlocate
-rwxr-xr-x 1 root root  249 ene 25  2018 passwd
-rwxr-xr-x 1 root root 3477 feb 21  2018 popularity-contest
-rwxr-xr-x 1 root root  246 mar 21  2018 ubuntu-advantage-tools
-rwxr-xr-x 1 root root  214 jun 27  2018 update-notifier-common

But if you need a specific schedule, just for a logrotate rule, the elected answer would be an option.

Upvotes: 1

Related Questions