I'm currently writing a program that prints to a Zebra printer. Because my office doesn't have a zebra printer, we print to a linux VM running netcat with nc -k -l -p 9100 | tee labels.txt so that we can view the output to the printer and verify correctness.

Unfortunately, this file gets pretty big and takes up a lot of space on the VM, especially because no one ever remembers to clear it.

Using tee seems to be a good option for writing to a file, but it isn't very featured in the way I'd desire. I'd like for the label.txt to only grow to a certain size (say 20 MB), at which point it begins overwriting itself. Or perhaps renames label.txt to label.txt.1, allowing label.txt to grow and then overwriting label.txt.1.

Is there any way to do this with netcat/tee? Or should I be looking at another program?


Answering your question, there is no feature in either netcat or tee to achieve this.

Maybe you could write a cron job, which runs every minute and checks the size of the label.txt, and when it reaches 20MB, clears the first 10MB.


There are a couple steps to what you want.

First, you'll need to put nc -k -l -p 9100 | tee labels.txt into a script, and add a handler for SIGHUP to restart the command.

Next, you'll need to setup logrotate to, well, rotate the logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.