I’ve got a DOS program kicked off by a cmd script that dumps a lot of data I’d like to track in a log file. I can easily pipe it to a file – but I need a rolling logfile.
Is there an easy way to pipe the output to a program that will generate a daily rolling logfile? (ie a new file is created for each day)
If you don’t control the source for the program you’re running, one possibility is to have another script, run as a scheduled task, which shuts down the program, moves the log file, and restarts the program. Schedule this for somewhere around midnight.
If you can’t shut down the program periodically, another possibility is to develop a filter program which will take standard input and send it to a log file based on today’s date. That program can detect date changes and close/reopen the file when it crosses the midnight boundary.
Then pipe the output of your original program through this filter. This is true piping by the way. What you’re currently doing is not technically piping but redirection (piping goes to a process, redirection goes to a file).
Pseudocode would be something like:
As a proof of concept, here’s a script (
qqtest.sh) that runs, generating the date every thirteen seconds, ten times:And here’s a C filter program (
qq.c) that does what I describe in my answer above:When you execute:
it creates the following files.
Note that this uses a minute boundary to switch log files. It’s a simple matter to change the
getDatefunction to use the day boundary instead (see the comments).