wait-for-it
Pure bash script to test and wait on the availability of a TCP host and port
Top Related Projects
./wait-for is a script to wait for another service to become available.
A simple script to wait for other docker images to be started while using docker-compose (or Kubernetes or docker stack or whatever)
Utility to simplify running applications in docker containers
Quick Overview
The vishnubob/wait-for-it
project is a simple Bash script that allows you to wait for a specific host and port to become available before executing a command. This can be useful in scenarios where you need to ensure that a dependent service or resource is ready before running your application.
Pros
- Simple and Lightweight: The script is a single Bash file, making it easy to integrate into your existing workflows.
- Cross-platform Compatibility: The script can be used on various Unix-like operating systems, including Linux and macOS.
- Customizable Behavior: The script allows you to configure various options, such as the timeout, retry interval, and the command to execute.
- Useful for Containerized Environments: The script can be particularly helpful in containerized environments, where you may need to wait for a database or other service to become available before starting your application.
Cons
- Bash-specific: The script is written in Bash, which may not be the preferred language for all users.
- Limited Error Handling: The script has relatively basic error handling, which may not be sufficient for more complex use cases.
- No Windows Support: The script is not designed to work on Windows operating systems, as it relies on Bash-specific features.
- Lack of Extensive Documentation: The project's documentation is relatively minimal, which may make it harder for new users to get started.
Code Examples
Here are a few examples of how you can use the wait-for-it.sh
script:
- Waiting for a TCP port to become available:
./wait-for-it.sh -h example.com -p 80 -- echo "Example.com is up and running!"
This command will wait for the TCP port 80 on the example.com
host to become available, and then execute the echo
command.
- Waiting for a TCP port with a custom timeout:
./wait-for-it.sh -h example.com -p 80 -t 60 -- echo "Example.com is up and running!"
This command is similar to the previous one, but it sets a custom timeout of 60 seconds.
- Waiting for a TCP port and executing a command:
./wait-for-it.sh -h example.com -p 80 -- ./start_my_app.sh
This command will wait for the TCP port 80 on the example.com
host to become available, and then execute the start_my_app.sh
script.
Getting Started
To use the wait-for-it.sh
script, follow these steps:
- Clone the GitHub repository:
git clone https://github.com/vishnubob/wait-for-it.git
- Navigate to the project directory:
cd wait-for-it
- Make the script executable:
chmod +x wait-for-it.sh
- Use the script in your own projects or scripts. For example:
./wait-for-it.sh -h example.com -p 80 -- echo "Example.com is up and running!"
This will wait for the TCP port 80 on the example.com
host to become available, and then execute the echo
command.
You can also customize the script's behavior by using the available command-line options, such as setting a custom timeout or retry interval.
Competitor Comparisons
./wait-for is a script to wait for another service to become available.
Pros of wait-for
- Written in Go, offering better performance and cross-platform compatibility
- Supports multiple hosts and ports simultaneously
- Provides more flexible timeout options, including custom timeout durations
Cons of wait-for
- Requires Go runtime or compilation, unlike the shell script-based wait-for-it
- May have a steeper learning curve for users unfamiliar with Go
- Less widespread adoption and community support compared to wait-for-it
Code Comparison
wait-for-it:
#!/usr/bin/env bash
wait_for_service() {
local host="$1"
local port="$2"
local timeout="${3:-15}"
until nc -z "$host" "$port" >/dev/null 2>&1
do
echo "Waiting for $host:$port..."
sleep 1
done
}
wait-for:
func waitForService(host string, port int, timeout time.Duration) error {
target := fmt.Sprintf("%s:%d", host, port)
for start := time.Now(); time.Since(start) < timeout; {
conn, err := net.DialTimeout("tcp", target, timeout)
if err == nil {
conn.Close()
return nil
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("timeout waiting for %s", target)
}
Both tools serve similar purposes, but wait-for offers more features and potentially better performance at the cost of increased complexity and setup requirements.
A simple script to wait for other docker images to be started while using docker-compose (or Kubernetes or docker stack or whatever)
Pros of docker-compose-wait
- Written in Rust, offering better performance and memory efficiency
- Supports multiple hosts and ports simultaneously
- Provides more detailed logging and customizable timeout options
Cons of docker-compose-wait
- Requires a separate binary to be included in the Docker image
- May have a steeper learning curve for users unfamiliar with Rust
- Less widespread adoption compared to wait-for-it
Code Comparison
wait-for-it:
#!/usr/bin/env bash
wait_for_service() {
local host="$1"
local port="$2"
local timeout="${3:-15}"
# ... (implementation details)
}
docker-compose-wait:
fn main() {
let args: Vec<String> = env::args().collect();
let hosts = parse_hosts(&args[1..]);
let timeout = parse_timeout(&args);
// ... (implementation details)
}
Both scripts serve the same purpose of waiting for services to become available before proceeding. wait-for-it uses Bash scripting, making it more accessible and easier to modify for users familiar with shell scripting. docker-compose-wait, written in Rust, offers improved performance and additional features but may require more specialized knowledge to customize.
Utility to simplify running applications in docker containers
Pros of dockerize
- More feature-rich, offering templating and log output in addition to waiting for services
- Written in Go, making it a single binary with no dependencies
- Supports waiting for multiple services simultaneously
Cons of dockerize
- Larger binary size compared to the lightweight shell script of wait-for-it
- May be overkill for simple use cases where only waiting functionality is needed
- Requires downloading a pre-built binary or building from source
Code comparison
wait-for-it:
#!/usr/bin/env bash
wait_for_service() {
local host="$1"
local port="$2"
local timeout="${3:-15}"
# ... (implementation details)
}
dockerize:
func waitForServices(services []string, timeout time.Duration) error {
var err error
for _, service := range services {
err = waitForService(service, timeout)
if err != nil {
return err
}
}
return nil
}
Both tools serve the purpose of waiting for services to become available, but dockerize offers a more comprehensive solution with additional features. wait-for-it is a simpler, lightweight option that may be sufficient for basic use cases. The choice between the two depends on the specific requirements of your project and the desired level of functionality.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
wait-for-it
wait-for-it.sh
is a pure bash script that will wait on the availability of a
host and TCP port. It is useful for synchronizing the spin-up of
interdependent services, such as linked docker containers. Since it is a pure
bash script, it does not have any external dependencies.
Usage
wait-for-it.sh host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
Examples
For example, let's test to see if we can access port 80 on www.google.com
,
and if it is available, echo the message google is up
.
$ ./wait-for-it.sh www.google.com:80 -- echo "google is up"
wait-for-it.sh: waiting 15 seconds for www.google.com:80
wait-for-it.sh: www.google.com:80 is available after 0 seconds
google is up
You can set your own timeout with the -t
or --timeout=
option. Setting
the timeout value to 0 will disable the timeout:
$ ./wait-for-it.sh -t 0 www.google.com:80 -- echo "google is up"
wait-for-it.sh: waiting for www.google.com:80 without a timeout
wait-for-it.sh: www.google.com:80 is available after 0 seconds
google is up
The subcommand will be executed regardless if the service is up or not. If you
wish to execute the subcommand only if the service is up, add the --strict
argument. In this example, we will test port 81 on www.google.com
which will
fail:
$ ./wait-for-it.sh www.google.com:81 --timeout=1 --strict -- echo "google is up"
wait-for-it.sh: waiting 1 seconds for www.google.com:81
wait-for-it.sh: timeout occurred after waiting 1 seconds for www.google.com:81
wait-for-it.sh: strict mode, refusing to execute subprocess
If you don't want to execute a subcommand, leave off the --
argument. This
way, you can test the exit condition of wait-for-it.sh
in your own scripts,
and determine how to proceed:
$ ./wait-for-it.sh www.google.com:80
wait-for-it.sh: waiting 15 seconds for www.google.com:80
wait-for-it.sh: www.google.com:80 is available after 0 seconds
$ echo $?
0
$ ./wait-for-it.sh www.google.com:81
wait-for-it.sh: waiting 15 seconds for www.google.com:81
wait-for-it.sh: timeout occurred after waiting 15 seconds for www.google.com:81
$ echo $?
124
Community
Debian: There is a Debian package.
Top Related Projects
./wait-for is a script to wait for another service to become available.
A simple script to wait for other docker images to be started while using docker-compose (or Kubernetes or docker stack or whatever)
Utility to simplify running applications in docker containers
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot