aboutsummaryrefslogtreecommitdiffstats
path: root/unix/findpid.sh
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2018-02-05 14:15:08 +0800
committerAaron LI <aly@aaronly.me>2018-02-05 14:15:08 +0800
commit9ba3e923040b6e1c4a5950d160aa62d42bf5bd87 (patch)
treece70361d3264ceb74dcb2ca426bd1f6bb7291710 /unix/findpid.sh
parent75017f26eb6657130c48921a747e829d02c0a9c3 (diff)
downloadatoolbox-9ba3e923040b6e1c4a5950d160aa62d42bf5bd87.tar.bz2
Add findpid.sh and waiton.sh tools
Diffstat (limited to 'unix/findpid.sh')
-rwxr-xr-xunix/findpid.sh28
1 files changed, 28 insertions, 0 deletions
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 }'