From 9ba3e923040b6e1c4a5950d160aa62d42bf5bd87 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Mon, 5 Feb 2018 14:15:08 +0800 Subject: Add findpid.sh and waiton.sh tools --- unix/findpid.sh | 28 ++++++++++++++++++++++++++++ unix/waiton.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 unix/findpid.sh create mode 100755 unix/waiton.sh (limited to 'unix') diff --git a/unix/findpid.sh b/unix/findpid.sh new file mode 100755 index 0000000..9ecd0dc --- /dev/null +++ b/unix/findpid.sh @@ -0,0 +1,28 @@ +#!/bin/sh +# +# Find the PID of a command in UNIX +# Jon Cairns +# http://blog.joncairns.com/2013/03/finding-the-process-id-pid-of-a-command-in-unix/ +# 2018-03-20 +# + +me="${0##*/}" +if [ $# -lt 1 ]; then + echo "$me: a grep expression for filtering processes required" >&2 + exit 1 +fi + +output=$(ps auxww | grep "$*" | grep -v grep | grep -v $0) +lines=$(echo "$output" | wc -l) + +if [ $lines -gt 1 ]; then + echo "$me: multiple processes matching the expression: '$*'" >&2 + echo + echo "$output" >&2 + exit 2 +elif [ -z "$output" ]; then + echo "$me: no processes matching the expression: '$*'" >&2 + exit 3 +fi + +echo "$output" | awk '{ print $2 }' diff --git a/unix/waiton.sh b/unix/waiton.sh new file mode 100755 index 0000000..67d88ac --- /dev/null +++ b/unix/waiton.sh @@ -0,0 +1,29 @@ +#!/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 +# + +case "$1" in + -h|--help|"") + echo "usage: ${0##*/} " >&2 + exit 1 + ;; +esac + +pid="$1" +me="${0##*/}($$)" +name=$(ps -p ${pid} -o comm=) +if [ $? -eq 0 ]; then + echo "${me}: waiting for process (${pid}, ${name}) to finish ..." + while ps -p ${pid} >/dev/null 2>&1; do + echo -n . + sleep 1 + done + echo +else + echo "${me}: failed to find process with PID ${pid}" >&2 + exit 2 +fi -- cgit v1.2.2