Some questions are answered here:
logrotate daily and size?
Typically logrotate
will only be run once daily, so the size limits will not be honoured exactly. logrotate
's status file (possibly /var/lib/logrotate.status
) only stores dates (not times), it is not intended for use more frequently, so you cannot trivially rotate files more frequently (Update: version 3.85 adds hourly support, and stores a complete timestamp in the state file.)
You don't say which syslogd
you're using, rsyslog and syslog-ng support self-managed size based rotation, so you should be able to get those to rotate by size, and get logrotate
to rotate weekly (though some thought might be required for file naming to make sure simultaneous file rotation doesn't accidentally delete something).
One other option is to use piped logs, just like Apache, in fact Apache-2.4's rotatelogs
supports exactly this feature (previous versions only supported size or time independently). You don't say where the logs are originating from, but you might be able to log to a pipe or fifo, and use rotatelogs
, if that's supported.
With logrotate
<= v3.8.0 the three supported scenarios are:
size
rotate by size, once daily at most, regardless of elapsed time period
- rotate unconditionally by time period (i.e.
daily
, weekly
, monthly
, yearly
), regardless of size
minsize
& time period
if logfile size exceed minsize, then rotate by time period (with `notifempty as a special case of this)
logrotate
v3.8.1 adds:
maxsize
& time period
rotate when either size exceeds maxsize, or after elapsed timeperiod. logrotate
may need to be run more than the default once per day in this case.
logrotate
v3.8.5 adds:
hourly
time period support, and storing a complete timestamp in the state file. You should run logrotate
(at least) hourly for this to work properly.
You can configure logrotate to rotate a file via copy-truncate, which preserves the original file and doesn't require you to re-create the log file.
The syntax is:
/tmp/output.log {
size 1M
copytruncate
rotate 4
compress
}
This will copy the current file, then cat /dev/null > file
That being said, logrotate only "automatically" rotates files in specific places, unless configured otherwise. I imagine you're logging your script to /var/log/httpd, and that's why it's being automatically rotated.
Best Answer
logrotate by itself doesn't support the splitting of the rotated log/s, only compression.
You may be able to get the desired behaviour by writing a script of your own that handles the splitting, and call that as a postrotate script in your logrotate configuration for that particular file/s.
update: based on your comment ("it is too late to split files, because they will be splitted by logrotate"), it seems you still have the idea that logrotate is doing splitting of files. This isn't correct, what actually happens is that logrotate renames the existing file and creates a new empty file with the original name.
An example:
When logrotate runs:
Hopefully you have a better understanding of what logrotate is doing now.
update 2: I'll address your comments here, as formatting options in the comments section are limited.
100% correct. After logrotate moves the file, rsyslog is still writing to the old file descriptor. A process performs I/O on the file descriptor, the name is only of interest when performing the initial open on the file.
No, hence the need to do it in a postrotate script. Remember that not all logging is necessarily done by a process which keeps an open file descriptor. If you have a process which opens a file, writes to it and then closes (the file descriptor) repeatedly, then you needn't fuss with anything in the postrotate.
No. I mean that it's still writing to the existing file descriptor, which logrotate has renamed to syslog.0. When dealing with processes like rsyslog, always remember that the file name is only relevant at the point when rsyslog opens it, i.e. when it creates the file descriptor. After that you can rename, move, even delete the file, and the process continues performing I/O on the file descriptor unhindered.