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:Explaining:
myprogram.shwrites to stdoutwhilebash sentence through a pipe|.while trueis an infinite loop that will never end, nor even whenmyprogram.shends which should break the pipe.headcommand is called to append the first 10 lines read from the pipe to the end of current/var/log/httpd/error_logfile (that may be different from the last loop because oflogrotate).(You can change the number of lines being written in each loop)
And another way is:
myprogram.shends or closes it's stdout.