When i first seen your post itself, it was interesting to me. I started a small research, but unsuccessful in that because of other reasons. But now got a good understanding, here is it, hope it will be useful for you.
Hope you understand the below code, if not, this is the explanation:
- A perl application which writes a number every one second, and also handles SIGINT, by flusing and closing stdout, and exiting.
$| = 1;
$i = 0;
$SIG{'INT'} = 'int_handler';
sub int_handler {
print STDERR "signal caught...\n";
close(stdout);
exit(0);
}
while (1) {
print STDOUT ++$i;
print STDERR $i;
sleep (1);
}
I executed this program, like this:
perl write.pl | split -b 3 -d -a 1 - "myappLog"
12
As expected, it started writing both in STDERR and also in STDOUT, so after some seconds i had given SIGINT like,
$ ps ax | grep write
5314 pts/0 S+ 0:00 perl write.pl
$ kill -INT 5314
So the following was the outcome,
$ perl write.pl | split -b 3 -d -a 1 - "myappLog"
12345678signal caught...
$
Tested whether everything was given to split, and does split processed ?
Yes.
$ ls myappLog*
myappLog0 myappLog1 myappLog2
$ head myappLog*
==> myappLog0 <==
123
==> myappLog1 <==
456
==> myappLog2 <==
78
Program wrote only 8 characters, and all are handled by split ( and it does not wait for 4096 as you have mentioned ).
So the final thing, you have to handle the signal and flush the output.
Hope it helps.