aboutsummaryrefslogtreecommitdiffstats
path: root/unix
diff options
context:
space:
mode:
Diffstat (limited to 'unix')
-rwxr-xr-xunix/waiton.sh120
1 files changed, 97 insertions, 23 deletions
diff --git a/unix/waiton.sh b/unix/waiton.sh
index 988d3d3..0689106 100755
--- a/unix/waiton.sh
+++ b/unix/waiton.sh
@@ -1,29 +1,103 @@
#!/bin/sh
#
-# Wait for a UNIX process to finish
-# Jon Cairns
-# http://blog.joncairns.com/2013/03/wait-for-a-unix-process-to-finish/
-# 2013-03-20
+# Copyright (c) 2018-2019 Aaron LI <aly@aaronly.me>
+# MIT License
#
+# Wait for process(s) to finish ...
+#
+# Credit: http://blog.joncairns.com/2013/03/wait-for-a-unix-process-to-finish/
+#
+
+me="${0##*/}"
+
+has_proc() {
+ ps -p $1 >/dev/null 2>&1
+}
+
+get_proc_cmd() {
+ if has_proc $1; then
+ ps -p $1 -o args=
+ else
+ echo "(not found)"
+ return 1
+ fi
+}
+
+usage() {
+ cat <<_EOF_
+
+Wait for process(s) to finish ...
+
+usage:
+ ${me} [-t interval] <pid1> [<pid2> ...]
+
+options:
+ -t : number of seconds between every check.
+ (default: 5)
+
+example:
+ ${me} 123 456 && command ...
-case "$1" in
- -h|--help|"")
- echo "usage: ${0##*/} <pid>" >&2
- exit 1
+_EOF_
+ exit 1
+}
+
+
+interval=5
+while getopts :ht: opt; do
+ case ${opt} in
+ h)
+ usage
+ ;;
+ t)
+ interval="${OPTARG}"
+ ;;
+ \?)
+ echo "${me}: invalid option -${OPTARG}"
+ usage
+ ;;
+ :)
+ echo "${me}: option -${OPTARG} requires an argument"
+ usage
;;
-esac
-
-pid="$1"
-me="${0##*/} (pid=$$)"
-name=$(ps -p ${pid} -o args=)
-if [ $? -eq 0 ]; then
- echo "${me}: waiting for process (pid=${pid}, ${name}) to finish ..."
- while ps -p ${pid} >/dev/null 2>&1; do
- echo -n .
- sleep 3
+ esac
+done
+
+shift $((${OPTIND} - 1))
+[ $# -eq 0 ] && usage
+
+echo "${me}: waiting for following process(s) to finish ..."
+pids="$@"
+nwait=0
+for p in ${pids}; do
+ cmd=$(get_proc_cmd ${p})
+ ret=$?
+ echo "* [pid: ${p}] ${cmd}"
+ if [ ${ret} -eq 0 ]; then
+ nwait=$((${nwait} + 1))
+ eval _has_${p}=yes
+ fi
+done
+
+echo -n "${me}: "
+while [ ${nwait} -ne 0 ]; do
+ nwait=0
+ for p in ${pids}; do
+ key="_has_${p}"
+ eval last=\$${key}
+ if has_proc ${p}; then
+ nwait=$((${nwait} + 1))
+ eval ${key}=yes
+ elif [ -n "${last}" ]; then
+ echo
+ echo "${me}: pid=${p} exited."
+ echo -n "${me}: "
+ eval ${key}=
+ fi
done
- echo
-else
- echo "${me}: failed to find process with PID ${pid}" >&2
- exit 2
-fi
+ echo -n "${nwait}."
+ sleep ${interval}
+done
+
+echo "done"
+exit 0