]> git.mxchange.org Git - friendica.git/blob - bin/wait-for-connection
Merge pull request #7938 from copiis/patch-2
[friendica.git] / bin / wait-for-connection
1 #!/usr/bin/php
2 #
3 # This script tries to connect to a database for a given interval
4 # Useful in case of installation e.g. to wait for the database to not generate unnecessary errors
5 #
6 # Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
7
8 <?php
9 $timeout = 60;
10 switch ($argc) {
11         case 4:
12                 $timeout = (float)$argv[3];
13         case 3:
14                 $host = $argv[1];
15                 $port = (int)$argv[2];
16                 break;
17         default:
18                 fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
19                 exit(2);
20 }
21 if ($timeout < 0) {
22         fwrite(STDERR, 'Timeout must be greater than zero'."\n");
23         exit(2);
24 }
25 if ($port < 1) {
26         fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
27         exit(2);
28 }
29 $socketTimeout = (float)ini_get('default_socket_timeout');
30 if ($socketTimeout > $timeout) {
31         $socketTimeout = $timeout;
32 }
33 $stopTime = time() + $timeout;
34 do {
35         $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
36         if ($sock !== false) {
37                 fclose($sock);
38                 fwrite(STDOUT, "\n");
39                 exit(0);
40         }
41         sleep(1);
42         fwrite(STDOUT, '.');
43 } while (time() < $stopTime);
44 fwrite(STDOUT, "\n");
45 exit(1);