#!/usr/bin/ksh #!/usr/bin/bash # flushwait: # wait until a detached command like amflush has completed # requires a POSIX compatible shell like ksh or bash # # Default program to wait for: amflush (specified with cmd line arg) # Default checking granularity: 60 seconds (time between checks - specified with -g ) # Default command to execute: none, but remaining arguments will be executed at end # uncomment for debugging # set -x # PS4='[$LINENO] ' ProgName=${0##*/} usage() { echo "Usage: ${ProgName} [-g ] [prog to wait for [cmd to run]]" >&2 exit 1 } # defaults ProgToWaitFor=amflush Granularity=60 # check for -g option case "${1}" in -g) Granularity=${2} shift shift ;; -g*) Granularity=${1#-g} shift ;; -*|+*) echo "${ProgName}: ${1}: Invalid option" >&2 usage ;; esac # check that granularity is numeric if echo "${Granularity}" | grep '^[0-9][0-9]*$' > /dev/null 2>&1 then : else echo "${ProgName}: ${Granularity}: granularity must be an integer" >&2 exit 1 fi # check for alternate program to wait for instead of amflush if [ "${1}" != "" ] then ProgToWaitFor="${1}" shift fi # Any remaining arguments will be executed at end, # after ProgToWaitFor completes # It is assumed that ProgToWaitFor is already running # Gets its process id number now. There could be multiple # instances; all must complete for this program to end ProgPid=$(ps -e | grep "[ /]${ProgToWaitFor}$" | awk '{print $1}') if [ "${ProgPid}" = "" ] then echo "${ProgName}: '${ProgToWaitFor}': no instance of this program running" >&2 exit 1 fi # echo PID is $ProgPid # debugging info # kill -0 tests for the existance of a the process # both kill and sleep are builtin commands in most posix shells # so no extra processes are generated by this loop while kill -0 ${ProgPid} 2> /dev/null do sleep ${Granularity} done # echo $ProgToWaitFor is complete "${@}"