pgrep
doesn't parse ouput of ps
, it will look through /proc/<PID>/status
and /proc/<PID>/cmdline
to finding matching for given pattern. Do some strace:
getpid() = 6572
stat("/proc/self/task", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
openat(AT_FDCWD, "/proc", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
getdents(3, /* 267 entries */, 32768) = 6792
stat("/proc/1", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
open("/proc/1/status", O_RDONLY) = 4
read(4, "Name:\tinit\nState:\tS (sleeping)\nT"..., 1023) = 750
close(4) = 0
open("/proc/1/cmdline", O_RDONLY) = 4
read(4, "/sbin/init", 2047) = 10
close(4) = 0
stat("/proc/2", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
open("/proc/2/status", O_RDONLY) = 4
read(4, "Name:\tkthreadd\nState:\tS (sleepin"..., 1023) = 518
close(4) = 0
open("/proc/2/cmdline", O_RDONLY) = 4
read(4, "", 2047) = 0
close(4) = 0
stat("/proc/3", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
open("/proc/3/status", O_RDONLY) = 4
read(4, "Name:\tksoftirqd/0\nState:\tS (slee"..., 1023) = 521
close(4) = 0
open("/proc/3/cmdline", O_RDONLY) = 4
read(4, "", 2047) = 0
close(4) = 0
stat("/proc/6", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
open("/proc/6/status", O_RDONLY) = 4
read(4, "Name:\tmigration/0\nState:\tS (slee"..., 1023) = 519
close(4) = 0
open("/proc/6/cmdline", O_RDONLY) = 4
read(4, "", 2047) = 0
close(4) = 0
By default, pgrep
only matches the process name (which is bash in your case). From man pgrep
:
-f The pattern is normally only matched against the process name.
When -f is set, the full command line is used.
So in your case, if you want to show script started via env
, try using:
pgrep -f foo.sh
#/usr/bin/env
kind of works too. I didn't get an error message any way when I ran it. – slm Aug 06 '13 at 17:56!
and much to my surprise it worked. – terdon Aug 06 '13 at 18:14