-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwaitfor
More file actions
executable file
·93 lines (84 loc) · 2.17 KB
/
waitfor
File metadata and controls
executable file
·93 lines (84 loc) · 2.17 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
#
# Wait until a process exits by PID.
#
# Accepts either --pid PID or a positional PID argument.
# If no PID is given, opens an interactive fzf picker from `ps ax`
# and infers the PID from the selected row.
#
# Useful for simple shell chaining, for example:
#
# waitfor 1234; echo 'process 1234 finished - do something after'
# waitfor; echo 'process finished!' # fzf-powered interactive process selection
#
# author: andreasl
show_usage() {
echo "Usage: ${0##*/} [--pid PID] [PID]"
echo
echo 'Wait until the target process exits.'
echo 'If no PID is provided, an interactive fzf picker opens.'
echo
echo 'Examples:'
echo " ${0##*/} --pid 1234"
echo " ${0##*/} 1234"
echo " ${0##*/}"
echo " ${0##*/} 1234; echo 'process finished - do something after'"
echo " ${0##*/}; echo 'interactively chosen process finished!'"
}
pid=''
while [ "$#" -gt 0 ]; do
case "$1" in
--pid)
shift
if [ -z "${1:-}" ]; then
echo 'Error: --pid requires a value.' >&2
exit 2
fi
pid="$1"
;;
--pid=*)
pid="${1#--pid=}"
if [ -z "$pid" ]; then
echo 'Error: --pid requires a value.' >&2
exit 2
fi
;;
-h | --help)
show_usage
exit 0
;;
-*)
echo "Error: unknown option '$1'." >&2
show_usage >&2
exit 2
;;
*)
if [ -n "$pid" ]; then
echo 'Error: PID specified more than once.' >&2
exit 2
fi
pid="$1"
;;
esac
shift
done
if [ -z "$pid" ]; then
if ! command -v fzf >/dev/null 2>&1; then
echo 'Error: fzf is required for interactive selection.' >&2
exit 127
fi
selection="$(ps ax -o pid= -o command= | fzf --prompt='waitfor> ' --header='Select process to wait for')"
[ -z "$selection" ] && {
echo 'No process selected.' >&2
exit 1
}
read -r pid _ <<EOF
$selection
EOF
fi
[[ ! "$pid" =~ ^[0-9]+$ ]] && {
echo "Error: PID must be numeric, got '$pid'." >&2
exit 2
}
tail -f /dev/null --pid="$pid"
printf '%s: Process %s finished.\n' "$(date)" "$pid"