1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#!/bin/sh
# Wait for a file to exist, up until a specified timeout.
#
# usage:
#
# $ wait-for-file <path-to-file> [<timeout-seconds>]
#
if [ $# -lt 1 ]; then
>&2 echo 'not enough arguments passed to wait-for-file'
exit 1
fi
file=$1
timeout=5
if [ $# -eq 2 ]; then
timeout=$2
elif [ $# -gt 2 ]; then
>&2 echo 'too many arguments passed to wait-for-file'
exit 1
fi
start=$(date +%s)
deadline=$((start + timeout))
now=$start
while ! [ -e "$file" ]; do
if [ "$now" -ge "$deadline" ]; then
>&2 echo "timed out waiting for file to exist: $file"
exit 2
fi
sleep 1
now=$(date +%s)
done
|