man logrotate
dateformat format_string
Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m
%d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the character sep‐
arating log name from the extension is part of the dateformat string. The system clock must be set
past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format
must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is
ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because
when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are
older and should be removed.
Can anyone please tell me if "dateext" is correct? I want the log filename to be something like "access.log-2010-12-04".
Insira uma diretiva dateformat
no seu arquivo de configuração, algo assim:
/usr/local/nginx/logs/*.log {
daily
dateext
dateformat -%Y-%m-%d
...
One more thing: Can I do the log rotation every day on a specific time (e.g. 11 pm)?
Por padrão, o logrotate está rodando via cron at 4 A.M:
/etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
Você pode mover esse arquivo para algum lugar e renomear para logrotate.sh
e, em seguida, criar um novo arquivo em /etc/cron.d/
como belows:
0 23 * * * root /path/to/logrotate.sh