Is there a tool to dynamically redirect output to a new file on request
I'll suggest a named pipe.
Create a pipe
mkfifo p
(call it whatever you want, if not 'p')Create a "reader" script that reads from the pipe and writes wherever you like
Tell the monitoring program to write its logs to the named pipe
Here's a sample reader script that reads from a named pipe 'p' and writes the data to an indexed 'mylog' file:
#!/bin/sh
INDEX=0
switchlog() {
read INDEX < newindex
echo now writing to "mylog.$INDEX"
}
trap switchlog USR1
while :
do
cat p >> mylog."$INDEX"
done
Building up on your SIGINT idea, here using SIGQUIT (Ctrl+\) to you can still use Ctrl+C to stop the whole thing:
(trap '' QUIT; monitor_command) | (
trap : QUIT
ulimit -c 0 # prevent core dump so SIGQUIT behaves like SIGINT
# for cat
n=0; while n=$((n+1)); file=output.$n.log; do
printf 'Outputting to "%s"\n' "$file"
cat > "$file"
done)
That assumes cat
is not a builtin in your shell (so it does get interrupted by you press Ctrl+\).
Note that like in your approach, there's a chance that the SIGQUIT be delivered at the wrong time (in the write system call) causing some data to be lost.