Skip to main content
added 112 characters in body
Source Link
kenorb
  • 26.9k
  • 29
  • 142
  • 210

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with ">>">> (append) instead of ">" > (create).

I ran into the same problem where the original file just keeps growing if you use >> (create) but if you use >>>> (append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.

Redirect STDOUT and STDERR to a rotating logfile:

  1. some-program.sh >> /tmp/output.txt 2>&1 &some-program.sh >> /tmp/output.txt 2>&1 &
  2. Create a logrotate config file under /etc/logrotate.d/etc/logrotate.d called whatever, output_roll in my case.

Sample config for my case:

/tmp/output.txt { notifempty missingok size 1G copytruncate start 0 rotate 15 compress }

    /tmp/output.txt {
        notifempty
        missingok
        size 1G
        copytruncate
        start 0
        rotate 15
        compress
    }
  1. setup your cron job inside the /etc/crontab file

    Setup your cron job inside the /etc/crontab file

     *  *  *  *  * root /usr/sbin/logrotate /etc/logrotate.d/output_roll
    
          • root /usr/sbin/logrotate /etc/logrotate.d/output_roll

This will check the file every minute. You can adjust to suit your needs.

  1. Start it up $>service crond restart:

     $> service crond restart
    
  2. That's it

NOTENote: I also had a problem with SELinux being set to SELINUX=enforcingSELINUX=enforcing so I set it to SELINUX=disabledSELINUX=disabled.

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with ">>" (append) instead of ">" (create).

I ran into the same problem where the original file just keeps growing if you use >(create) but if you use >>(append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.

Redirect STDOUT and STDERR to a rotating logfile:

  1. some-program.sh >> /tmp/output.txt 2>&1 &
  2. Create a logrotate config file under /etc/logrotate.d called whatever, output_roll in my case.

Sample config for my case:

/tmp/output.txt { notifempty missingok size 1G copytruncate start 0 rotate 15 compress }

  1. setup your cron job inside the /etc/crontab file
          • root /usr/sbin/logrotate /etc/logrotate.d/output_roll

This will check the file every minute. You can adjust to suit your needs.

  1. Start it up $>service crond restart

  2. That's it

NOTE: I also had a problem with SELinux being set to SELINUX=enforcing so I set it to SELINUX=disabled.

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with >> (append) instead of > (create).

I ran into the same problem where the original file just keeps growing if you use > (create) but if you use >> (append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.

Redirect STDOUT and STDERR to a rotating logfile:

  1. some-program.sh >> /tmp/output.txt 2>&1 &
  2. Create a logrotate config file under /etc/logrotate.d called whatever, output_roll in my case.

Sample config for my case:

    /tmp/output.txt {
        notifempty
        missingok
        size 1G
        copytruncate
        start 0
        rotate 15
        compress
    }
  1. Setup your cron job inside the /etc/crontab file

     *  *  *  *  * root /usr/sbin/logrotate /etc/logrotate.d/output_roll
    

This will check the file every minute. You can adjust to suit your needs.

  1. Start it up:

     $> service crond restart
    
  2. That's it

Note: I also had a problem with SELinux being set to SELINUX=enforcing so I set it to SELINUX=disabled.

added 731 characters in body
Source Link

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with ">>" (append) instead of ">" (create).

I ran into the same problem where the original file just keeps growing if you use >(create) but if you use >>(append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.

Redirect STDOUT and STDERR to a rotating logfile:

  1. some-program.sh >> /tmp/output.txt 2>&1 &
  2. Create a logrotate config file under /etc/logrotate.d called whatever, output_roll in my case.

Sample config for my case:

/tmp/output.txt { notifempty missingok size 1G copytruncate start 0 rotate 15 compress }

  1. setup your cron job inside the /etc/crontab file
          • root /usr/sbin/logrotate /etc/logrotate.d/output_roll

This will check the file every minute. You can adjust to suit your needs.

  1. Start it up $>service crond restart

  2. That's it

NOTE: I also had a problem with SELinux being set to SELINUX=enforcing so I set it to SELINUX=disabled.

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with ">>" (append) instead of ">" (create).

I ran into the same problem where the original file just keeps growing if you use >(create) but if you use >>(append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with ">>" (append) instead of ">" (create).

I ran into the same problem where the original file just keeps growing if you use >(create) but if you use >>(append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.

Redirect STDOUT and STDERR to a rotating logfile:

  1. some-program.sh >> /tmp/output.txt 2>&1 &
  2. Create a logrotate config file under /etc/logrotate.d called whatever, output_roll in my case.

Sample config for my case:

/tmp/output.txt { notifempty missingok size 1G copytruncate start 0 rotate 15 compress }

  1. setup your cron job inside the /etc/crontab file
          • root /usr/sbin/logrotate /etc/logrotate.d/output_roll

This will check the file every minute. You can adjust to suit your needs.

  1. Start it up $>service crond restart

  2. That's it

NOTE: I also had a problem with SELinux being set to SELINUX=enforcing so I set it to SELINUX=disabled.

Source Link

I just wanted to add to Sam Hendley's comment above:

The trick is this only works if the redirect is done with ">>" (append) instead of ">" (create).

I ran into the same problem where the original file just keeps growing if you use >(create) but if you use >>(append) Logrotate copytruncate works beautifully and as expected. The original file goes back down to zero bytes and the program continues writing.