4 # Try to detect the PID of a running process of the current user.
11 echo "Usage: $0 <name>" >&2
17 # Use pgrep(1) whenever possible
18 if [ -x /usr/bin/pgrep ]; then
26 if [ -n "${LOGNAME:-}" ] || [ -n "${USER:-}" ]; then
27 # Try to narrow the search down to the current user ...
28 exec /usr/bin/pgrep $PGREP_FLAGS -n -u "${LOGNAME:-$USER}" "$1"
30 # ... but neither LOGNAME nor USER were set!
31 exec /usr/bin/pgrep $PGREP_FLAGS -n "$1"
35 # pidof(1) could be a good alternative on elder Linux systems
36 if [ -x /bin/pidof ]; then
37 exec /bin/pidof -s "$1"
40 # fall back to ps(1) and parse its output:
41 # detect flags for "ps" and "head"
45 PS_FLAGS="-a"; PS_PIDCOL=2
48 PS_FLAGS="-o Id -o Team"
51 # Linux (GNU coreutils), Free/Net/OpenBSD, ...
52 PS_FLAGS="-o pid,comm"
56 ps $PS_FLAGS >procs.tmp
57 grep -v "$$" procs.tmp | grep "$1" | \
58 awk "{print \$$PS_PIDCOL}" | \
60 pid=`head -1 pids.tmp`
61 rm -rf procs.tmp pids.tmp
64 [ "$pid" -gt 1 ] >/dev/null 2>&1 || exit 1