Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some applications in Linux which create log files of around 41 MB each. The application switches from log.1 to log.2 and starts overwriting log.2 once this 41MB is completed in log.1 file.

I want to take the backup of these files in the way that whenever the new file log.2 reaches the size more than 1MB it take backup (gzip and store at another location) of other file (log.1).

I have been using a script which takes the backup of a file whenever it reaches 41MB but the trouble here is sometimes this 41MB file backup is processed by the script while the data is still getting written to the same file at a huge rate. Because of that some seconds of log data are lost which is the reason I am trying the alternate way of making backup of other file once the new file reaches 1MB or something, so that I am sure that the other file is finished writing.

Any idea how do I do that?

share|improve this question
Have you considered using rsyslog to log over the network to a remote machine? You can than take backups/snapshots over there and control the rotation of the files without disturbing this machine. – gertvdijk Dec 27 '12 at 11:23
May i know more about rsyslog? – Ankit Vashistha Dec 27 '12 at 11:28
I second the comment about rsyslog. There's more information about it at rsyslog.com – Jenny D Dec 27 '12 at 12:11
I think that's not something which can solve my problem. – Ankit Vashistha Dec 27 '12 at 12:54
Configure the chatterbox to shut up? Nobody will really read all those logs, do they. – vonbrand Jan 22 '13 at 1:12

This can be done through logrotate , see below example for apache logs

Step 1# First make sure logrotate is installed in your system, by default is available on almost all distro of Linux

Step 2# Create /etc/logrotate_apache.conf as below

/var/log/httpd/*log {
    minsize 1M
    rotate 1
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
        Log_dir="/var/log/httpd/Apache_$(date +%F)/"
        # move to other location
        [[ ! -d "${Log_dir}" ]] && /bin/mkdir "${Log_dir}" &&  /bin/mv /var/log/httpd/*.gz "${Log_dir}"

# system-specific logs may be also be configured here.

Step 3# Set Cron task as below

00 * * * * /usr/sbin/logrotate -f /etc/logrotate_apache.conf
share|improve this answer

You can try logrotate, Some Examples are mentioned here

The size option will let you rotate the log once it reaches a specified size, say 41MB.
Use the postrotate option, to execute a custom script which you can use to transfer the rotated log file to another location.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.