#!/bin/bash # wait in bash only works for processes that are direct children to the calling # shell. This script is more general purpose. wait_pid() { local pid=$1 local max_attempts=600 # 600 * 0.1 seconds = 1 Minute local attempt=0 local sleep_time=0.1 while [ $attempt -lt $max_attempts ]; do if [[ "$OSTYPE" == "msys"* || "$OSTYPE" == "cygwin"* ]]; then # Windows approach if ! tasklist | grep -q $pid; then echo "[wait_pid] process has ended" return 0 fi else # Linux/macOS approach if ! kill -0 $pid 2>/dev/null; then echo "[wait_pid] process has ended" return 0 fi fi sleep $sleep_time attempt=$((attempt + 1)) done echo "Timeout: Process $pid did not end within 1 minute" return 1 } # Usage if [ $# -eq 0 ]; then echo "Usage: $0 " exit 1 fi wait_pid $1 exit $?