aboutsummaryrefslogtreecommitdiffstats
path: root/unix/findpid.sh
blob: 9ecd0dc9a8de83905f3e6e4a1a67bc1a799c740b (plain)
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
#!/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 }'