I am trying to redirect the output of a continuously running program to a file say error_log
.
The command I am using looks like this, where myprogram.sh
runs continuously generating data and writing to /var/log/httpd/error_log
myprogram.sh >> /var/log/httpd/error_log
Now I am using logrotate
to rotate this file per hour. I am using create
command in logrotate
so that it renames the original file and creates a new one.
The logrotate
config looks like
/var/log/httpd/error_log {
# copytruncate
create
rotate 4
dateext
missingok
ifempty
.
.
.
}
But here redirection fails. What I want is myprogram.sh
to write data to error_log
file irrespective of it being rotated by logrotate, obviously to newly generated error_log
file
Any idea how to make redirection work based on the file name and not the descriptor ?
OR
Any other way of doing it in bash ?
If I understood your problem, one solution (without modify myprogram.sh
) could be:
$ myprogram.sh | while true; do head -10 >> /var/log/httpd/error_log; done
Explaining:
myprogram.sh
writes to stdoutwhile
bash sentence through a pipe |
.while true
is an infinite loop that will never end, nor even when myprogram.sh
ends which should break the pipe.head
command is called to append the first 10 lines read from the pipe to the end of current /var/log/httpd/error_log
file (that may be different from the last loop because of logrotate
).(You can change the number of lines being written in each loop)
And another way is:
$ myprogram.sh | while read line; do echo "$line" >> /var/log/httpd/error_log; done
myprogram.sh
ends or closes it's stdout.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With