7

Let's say I want to know the PID of kate editor that is currently running (kate is just an example, it works as described for many programs). So, I execute ps aux | grep kate.

Here is the output:

seifert     35523  0.0  0.2 448724 25144 ?        Sl   08:56   0:33 /bin/kate -b
seifert   1499179  0.0  0.0  12196   656 pts/0    S+   21:52   0:00 grep --color=auto kate

The first line is what I expect to see. But what is the second one?

0

3 Answers 3

12

If you type ps aux|grep kate, this will launch grep kate, grep kate will be seen as grep kate (or something more complex if you have some aliases like in this example). And grep kate will select the corresponding line because there is a kate substring.

If you want to get kate process from the list of processes BEFORE the grep command, pgrep can be the command you need.

EDIT: Even pgrep pgrep doesn't find itself.

0
5

That's because it return one line for the process you are searching for AND for the command you just entered: grep kate.

So, if you want to have just one response use this command:

ps -fp $(pgrep -f '<your_search>')

pgrep -f searches the PID of a process based on its launching full command (parameters included).

2

Quoting Wayne Werner's answer:

By putting the brackets around the letter and quotes around the string you search for the regex, which says, "Find the character 'f' followed by 'nord'." But since you put the brackets in the pattern 'f' is now followed by ']', so grep won't show up in the results list. Neato!

With [f] i don't get the last line with the grep command it self in the output right diff: beetwen "ps aux | grep firefox" and "ps aux | grep [f]irefox" is the last output line

How can I prevent 'grep' from showing up in ps results?

If you don't want so see the last grep command you be called like ps aux | grep 'kate' process/command in the process list you can use

ps aux | grep '[k]ate'

you can use this brackets [] with every command/process you searching

ps aux | grep '[c]ommand' or '[p]rocess'

You can also use, to get only the pid

pidof kate

or

pgrep kate

or

ps -ef | grep [k]ate

0

Not the answer you're looking for? Browse other questions tagged .