'Netcat should auto reconnect on disconnection

I have a problem with network interruptions causing netcat to disconnect and then I have to rerun the script.

(/usr/bin/php < dummyInput.txt textGen.php | /usr/bin/nc 192.168.3.4 2001 >/dev/null &)

How can I make the above reconnect and continue after the network connection is broken and netcat exits?

This question Netcat auto reconnect on disconnection is similar but is not really answered.



Solution 1:[1]

You can use:

while [ 1 ]; do php < dummyInput.txt textGen.php | nc 192.168.3.4 2001 >/dev/null; sleep 5; done &

I added sleep 5 to slow down re-connection attempts. Set that to the number of seconds that makes sense for your situation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xavier Guihot