databricks-cli/acceptance/bin/wait_pid

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
1003 B
Plaintext
Raw Normal View History

2025-02-17 18:23:02 +00:00
#!/bin/bash
# wait <pid> 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
2025-02-18 13:13:01 +00:00
local max_attempts=600 # 600 * 0.1 seconds = 1 Minute
2025-02-17 18:23:02 +00:00
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
2025-02-18 12:14:29 +00:00
echo "[wait_pid] process has ended"
2025-02-17 18:23:02 +00:00
return 0
fi
else
# Linux/macOS approach
if ! kill -0 $pid 2>/dev/null; then
2025-02-18 12:14:29 +00:00
echo "[wait_pid] process has ended"
2025-02-17 18:23:02 +00:00
return 0
fi
fi
sleep $sleep_time
attempt=$((attempt + 1))
done
2025-02-18 13:51:03 +00:00
echo "Timeout: Process $pid did not end within 1 minute"
2025-02-17 18:23:02 +00:00
return 1
}
# Usage
if [ $# -eq 0 ]; then
echo "Usage: $0 <PID>"
exit 1
fi
wait_pid $1
exit $?