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 exec /usr/bin/pgrep $PGREP_FLAGS -n -u "$LOGNAME" "$1"
29 # pidof(1) could be a good alternative on elder Linux systems
30 if [ -x /bin/pidof ]; then
31 exec /bin/pidof -s "$1"
34 # fall back to ps(1) and parse its output:
35 # detect flags for "ps" and "head"
39 PS_FLAGS="-a"; PS_PIDCOL=2
42 PS_FLAGS="-o Id -o Team"
45 # Linux (GNU coreutils), Free/Net/OpenBSD, ...
46 PS_FLAGS="-o pid,comm"
50 ps $PS_FLAGS >procs.tmp
51 grep -v "$$" procs.tmp | grep "$1" | \
52 awk "{print \$$PS_PIDCOL}" | \
54 pid=`head -1 pids.tmp`
55 rm -rf procs.tmp pids.tmp
58 [ "$pid" -gt 1 ] >/dev/null 2>&1 || exit 1