How to make netcat to save data to a file?

There is a service that periodically sends packets to the server on a specific port. I want to put netcat on it to all incoming packets to save in the specified file in the background.
If you run
nc-k -lp 8080 >> data.txt &

It takes a single packet and exits.

If you run this:
while true; do nc-k -lp 8080 >> data.txt; done &

The process accepts and records a single package, displays in the console something like:

[2]+ Stopped while true; do
nc-k -lp 8080 >> data.txt;
done


continues to hang in processes, but in the file nothing write.
How to run netcat to hang in the background and continued to write packets to a file?

The system is Debian GNU/Linux 9

P. S. While I decided it's not running in the background, and screen. But I would like a more elegant solution.
April 19th 20 at 12:15
2 answers
April 19th 20 at 12:17
Solution
Decided independently. This code works correctly, plus as a bonus - in the log are time stamped together with the dump package:

while true; do (date; nc -k-lp 8080 2>&1 | sed 's/\r//g') | xargs >> environ.txt; done &
April 19th 20 at 12:19
man tee
while true; do nc-k -lp 8080 | tee-ai data.txt; done &


One writing the package file. The process was left hanging. New packages is not recorded. - kennedi commented on April 19th 20 at 12:22

Find more questions by tags Linux