Skip to content

Instantly share code, notes, and snippets.

@steezeburger
Last active March 31, 2023 23:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steezeburger/ec4fccf9038d352ab71002a90cfad49d to your computer and use it in GitHub Desktop.
Save steezeburger/ec4fccf9038d352ab71002a90cfad49d to your computer and use it in GitHub Desktop.
Similar to the popular wait-for-it.sh that waits for a network connection, this bash script waits for a file to exist. It does not have the same api as wait-for-it.sh, but could be extended easily enough to be the same.
#!/bin/bash
# Usage: ./wait-for-file.sh /path/to/your/file
FILE_PATH="$1"
TIMEOUT=30
SLEEP_INTERVAL=1
ELAPSED=0
if [ -z "$FILE_PATH" ]; then
echo "Usage: $0 /path/to/your/file"
exit 1
fi
echo "Waiting for file: $FILE_PATH"
while [ ! -f "$FILE_PATH" ]; do
sleep $SLEEP_INTERVAL
ELAPSED=$((ELAPSED + SLEEP_INTERVAL))
if [ $ELAPSED -ge $TIMEOUT ]; then
echo "Timeout reached, file not found."
exit 1
fi
done
echo "File is now available: $FILE_PATH"
# Continue with your script here...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment