#!/usr/bin/php # # This script tries to connect to a database for a given interval # Useful in case of installation e.g. to wait for the database to not generate unnecessary errors # # Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}] $timeout) { $socketTimeout = $timeout; } $stopTime = time() + $timeout; do { $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout); if ($sock !== false) { fclose($sock); fwrite(STDOUT, "\n"); exit(0); } sleep(1); fwrite(STDOUT, '.'); } while (time() < $stopTime); fwrite(STDOUT, "\n"); exit(1);