Tracking process via ps command

Hello folks,

I am tracking a process httpd only. But when i am grepping it, it is returning me multiple process of httpd, second it is showing another process of monitorix-httpd. Below commands i have tried.

Current output

# ps ax |grep http
  929 ?        Ss     0:00 monitorix-httpd listening on 8080
 9551 ?        Ss     0:00 /usr/sbin/httpd -k start
 9552 ?        S      0:00 /usr/sbin/httpd -k start
 9554 ?        S      0:00 /usr/sbin/httpd -k start
 9555 ?        S      0:00 /usr/sbin/httpd -k start
 9556 ?        S      0:00 /usr/sbin/httpd -k start
 9557 ?        S      0:00 /usr/sbin/httpd -k start
 9558 ?        S      0:00 /usr/sbin/httpd -k start
 9559 ?        S      0:00 /usr/sbin/httpd -k start
 9560 ?        S      0:00 /usr/sbin/httpd -k start
 9561 ?        S      0:00 /usr/sbin/httpd -k start

Expected output: one process that confirms that httpd is running, not all httpd child process and neither monitorx process.

expected output


httpd

Why not use pgrep ?

pgrep -nf /usr/sbin/httpd

This will return you the pid of the latest httpd process.

--ahamed

1 Like

If i don't want to mention the path. Below command is fine?


# pgrep  -n ^httpd$
9561

# echo $?
0

cross-checking

# pgrep  -n ^httpd1$


# echo $?
1


Yes, that should do.
And btw you can check the output of pgrep directly rather than checking the exit status.

--ahamed

1 Like

Thanks it is clear now.

pgrep '^httpd$'

is the same as

pgrep -x httpd

Instead of -n (newest) I would take -o (oldest), or pick the one that is spawned by init (PID 1)

pgrep -P 1 -x httpd

NB in Solaris zones the latter does not work because init is not 1 (and even got a different name).

1 Like