-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pwait
executable file
·60 lines (51 loc) · 1.11 KB
/
pwait
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
##
## Solaris pwait(1) clone for Linux, AIX
## Copyright (c) 2012-2018 SATOH Fumiyasu @ OSS Technology Corp., Japan
## <https://github.com/fumiyas/home-commands/blob/master/pwait>
## <https://fumiyas.github.io/>
##
## License: GNU General Public License version 3
##
perr()
{
echo "$0: ERROR: $1" 1>&2
}
verbose_p=""
poll_interval="${PWAIT_POLL_INTERVAL:-10}"
if [ "${1-}" = "-v" ]; then
verbose_p="set"
shift
fi
if [ $# -eq 0 ]; then
echo "Usage: $0 [-v] PID ..."
exit 1
fi
if type inotifywait >/dev/null 2>&1; then
inotifywait="set"
else
inotifywait=""
fi
trap 'rc=$?; trap "" TERM; kill -TERM -$$; exit $rc' EXIT
for pid in "$@"; do
if ! cd "/proc/$pid" 2>/dev/null; then
perr "No such process: $pid"
continue
fi
(
[ -r exe ] || inotifywait=""
if [ -n "$inotifywait" ]; then
set -- inotifywait --quiet --event close_nowrite exe
else
set -- sleep "$poll_interval"
fi
while :; do
"$@" >/dev/null
if ! cd . 2>/dev/null; then
[ -n "$verbose_p" ] && echo "$pid: terminated"
exit 0
fi
done
) &
done
wait