File: wait-for-file

package info (click to toggle)
dd-opentracing-cpp 1.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,336 kB
  • sloc: cpp: 44,895; sh: 697; ansic: 27; makefile: 20
file content (35 lines) | stat: -rwxr-xr-x 613 bytes parent folder | download | duplicates (2)
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