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:
- some-program.sh >> /tmp/output.txt 2>&1 &
some-program.sh >> /tmp/output.txt 2>&1 & - Create a logrotate config file under /etc/logrotate.d
/etc/logrotate.dcalled 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
}
- setup your cron job inside the /etc/crontab file
Setup your cron job inside the
/etc/crontabfile* * * * * 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.
Start it up $>service crond restart:
$> service crond restartThat's it
NOTENote: I also had a problem with SELinux being set to SELINUX=enforcingSELINUX=enforcing so I set it to SELINUX=disabledSELINUX=disabled.