Redirecting program output to a rotating file

I am trying to redirect program output continuously to a file, say error_log.

The command I use looks like this: it myprogram.shworks continuously, generating data and writing to/var/log/httpd/error_log

  • myprogram.sh >> /var/log/httpd/error_log

Now I use logrotateto rotate this file per hour. I use the command createin logrotateto rename the original file and create a new one. Configuration logrotatelooks like

/var/log/httpd/error_log {
# copytruncate
 create
 rotate 4
 dateext
 missingok
 ifempty
 .
 .
 .
}

But the redirection is reassigned. I want to myprogram.shwrite data to a file error_logno matter what it rotates with logrotate, obviously for a newly created fileerror_log

, ?

bash?

+4
1

, ( myprogram.sh) :

$ myprogram.sh | while true; do head -10 >> /var/log/httpd/error_log; done

:

  • myprogram.sh stdout
  • while bash |.
  • while true - , , myprogram.sh , .
  • head 10 , /var/log/httpd/error_log ( - logrotate).

( , )

:

$ myprogram.sh | while read line; do echo "$line" >> /var/log/httpd/error_log; done
  • , , myprogram.sh stdout.
  • .
+2

Source: https://habr.com/ru/post/1664712/


All Articles