Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process grep with pipe returns itself. How do I exclude it? [duplicate]

Tags:

linux

bash

root@test:~# ps x | grep 'vsftpd'
  568 ?        Ss     0:00 /usr/sbin/vsftpd
28694 pts/0    S+     0:00 grep --color=auto vsftpd

How can I exclude the self grep process itself? Also, how do I fetch the process Id (pid) given a part of the name of the process?

I am looking for something along the lines that it will give me pid given the name and excludes the self grep process.

like image 711
user1757703 Avatar asked Dec 06 '22 01:12

user1757703


2 Answers

The usual trick is

ps x | grep '[v]sftpd'
like image 124
choroba Avatar answered May 11 '23 15:05

choroba


The traditional way would be:

ps x | grep 'vsftpd'| grep -v grep

in which grep -v expr returns everything not matching expr

You can then use awk to extract the relevant field (the pid in your case)

ps x | grep 'vsftpd'| grep -v grep | awk '{ print $2 }'

(the $2 corresponds to the relevant field/column)

like image 43
Brian Agnew Avatar answered May 11 '23 13:05

Brian Agnew