Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrian-gheorghe/wait
This script waits for a host or multiple hosts to respond on a TCP port but can also wait for a command to output a value.
https://github.com/adrian-gheorghe/wait
bash bash-script shell tcp
Last synced: 2 months ago
JSON representation
This script waits for a host or multiple hosts to respond on a TCP port but can also wait for a command to output a value.
- Host: GitHub
- URL: https://github.com/adrian-gheorghe/wait
- Owner: adrian-gheorghe
- License: mit
- Created: 2018-09-19T20:56:25.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-29T05:36:01.000Z (over 4 years ago)
- Last Synced: 2024-02-13T21:56:04.748Z (11 months ago)
- Topics: bash, bash-script, shell, tcp
- Language: Shell
- Homepage:
- Size: 8.79 KB
- Stars: 11
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## wait.sh
`wait.sh` is a bash script inspired by https://github.com/vishnubob/wait-for-it and https://github.com/eficode/wait-for
The script waits for a host or multiple hosts to respond on a TCP port but can also wait for a command to output a value. For example you can wait for a file to exist or contain something.The script is mainly useful to link containers that dependend on one another to start. For example you can have a container that runs install scripts that will have to wait for the database to be accessible.
## Requirements
netcat - The machine / container running wait.sh needs to have the netcat service installed.## Usage
```
./wait.sh --help
wait.sh [[-w | --wait "host:port"] | [[-w | --wait "ls -al /var/www"] | [[-c | --command "printenv"] | [[-t | --timeout 10] | [-h | --help]]
-w "HOST:PORT" | --wait "HOST:PORT" You can specify the HOST and TCP PORT to test
-w "ls -al /var/www" | --wait "ls -al /var/www" Alternatively you can specify a bash command that should return something
-c "printenv" | --command "printenv" Command that should be run when all waits are accessible. Multiple commands can be added
-t TIMEOUT | --timeout=TIMEOUT Timeout untill script is killed. Not interval between calls
-i INTERVAL | --interval=INTERVAL Interval between calls
-h | --help Usage / Help
```## Examples shell
```
$ ./wait.sh --wait "database_host:3306" --wait "ls -al /var/www/html | grep docker-compose.yml" --command "Database is up and files exist"
$ ./wait.sh --wait "database_host:3306" --wait "database_host2:3306" --command "echo \"Databases are up\""
```You can set your own timeout with the `-t` or `--timeout=` option. Setting the timeout value to 0 will disable the delay between requests:
```
$ ./wait.sh --wait "database_host:3306" --wait "database_host2:3306" --command "echo \"Databases are up\"" --timeout 15
```
## Examples docker-compose```
version: '3.3'
services:
db:
image: mysql:5.7
deploy:
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
wait:
build:
context: .
command: "./wait.sh --wait \"db:3306\" --command \"ls -al\""
```## Example docker multiple FROM
In the following example the Dockerfile adds the wait.sh file from the adighe/wait container.
The setup allows the running of database migrations only after the database is accessible and the volume is mounted### Dockerfile
```FROM adighe/wait as wait
FROM php:7.1.3-fpm# Install dependencies
RUN apt-get update \
&& apt-get install -y \
netcatRUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composerCOPY --from=wait /app/wait.sh /app/wait.sh
ENTRYPOINT ["docker-php-entrypoint"]
CMD ["php-fpm"]
```
### docker-compose.yml
```version: '3.3'
services:
db:
image: mysql:5.7
deploy:
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
install:
build:
context: .
command: "/bin/bash -c \"/app/wait.sh --wait 'db:3306' --wait 'ls -al /var/www/html/ | grep composer.json' --command 'cd /var/www/html' --command 'ls -al' --command 'composer install' --command 'php /var/www/html/bin/console doctrine:migrations:migrate -n -vvv'\""```