Given a bash command line of the form
commandA | commandB
I want to add a buffer of size ~1MB that sits between commandA and <
alternatively you could use a named pipe and run them in parallel:
mkfifo myfifo
commandB < myfifo &
commandA > myfifo
rm myfifo
BUFFER is called buffer. (man 1 buffer, maybe after apt-get install buffer)
The program buffer uses shared memory. This might be a problem, because in case of an error, memory may leak, because shared memory can outlive the program, which allocated the memory.
An alternative may be GNU dd:
commandA |
dd status=none iflag=fullblock bs=1M |
commandB
It is important to use the fullblock option. Otherwise dd may cause data loss, when reading from a pipe.
Parameters of dd explained
status=none
Set the level of information to print to stderr; 'none' suppresses everything but error messages
iflag=fullblock
accumulate full blocks of input
bs=1M
read and write up to one Mega bytes at a time (default: 512 bytes);
You can use
E.g.
process1 | mbuffer -m 1024M | process2
to use a 1G buffer
There is another tool, pv - pipe viewer:
process1 | pv -pterbTCB 1G | process2
B specifies the buffer size, here 1 GigibyteC disables splice, which is required for BT shows the buffer levelpterb are the default display switches needed due to the presence of Tpv might be available on systems where mbuffer/buffer is not in the official repositories (such as arch linux).