wait-for-it
wait-for-it copied to clipboard
Add a timeout on connecting to a TCP device
In our setup (running inside docker), opening a connection to a TCP device has a long timeout (128 seconds). That means that the script will start by trying to connect to the device. If this is not immediately succesful, it takes 128 for the command to fail. So if the device is actualy available after 10 seconds, we wait 118 seconds too much.
This setting is defined in the source, and is is not possible to change this timeout in bash. A workaround is to add a timeout to the command attempting a connection to the device.
This solution is based on https://stackoverflow.com/a/40158514
Also, script can be changed to prefer using nc command, or even netcat or ncat if any of those commands exist, and not just when BusyBox is used (this is how the script is currently implemented).
Furthermore, -w flag (which stands for timeout) set to the value of e.g. 1 second needs to be added to nc command in the script, as traditional netcat implementation has no default timeout, and newer Ncat implementation has a default timeout of 10 seconds, meaning that without the -w flag, the wait-for-it.sh script would behave very differently depending on which nc is installed.
Hey @dennisschroer
You can use the Wait4X, It's already supported TCP connection timeout.
Example:
wait4x tcp 127.0.0.1:80 --connection-timeout=10s --timeout=500s