2

I use the ps -ef to check the process, but every time the pid will change, you see the below, 1726-> 1728->...

aircraftdeMacBook-Pro:etc ldl$ ps -ef | grep safira
  501  1726   527   0 10:14上午 ttys000    0:00.00 grep safira
aircraftdeMacBook-Pro:etc ldl$ ps -ef | grep safira
  501  1728   527   0 10:14上午 ttys000    0:00.00 grep safira
aircraftdeMacBook-Pro:etc ldl$ ps -ef | grep safira
  501  2298   527   0 11:18上午 ttys000    0:00.00 grep safira
aircraftdeMacBook-Pro:etc ldl$ ps -ef | grep safira
  501  2301   527   0 11:18上午 ttys000    0:00.00 grep safira
1

Every time you run this in Terminal two new processes for ps and grep are created, run to do their job and then terminated successfully. So each invocation gets new PIDs.


To properly exclude the process grep search_string from grep'ing for search_string in the ps result use:

ps -ef | grep -v grep | grep search_string

or

ps -ef | grep [s]earch_string

e.g. ps -ef | grep -v grep | grep safira or ps -ef | grep [s]afira

0

The ps ... | grep ... pipeline can often be replaced with pgrep (or pkill) which unlike the pipeline are clever enough not to match themselves:

pgrep safira
  • I use the pgrep safira, gets nothing. – aircraft Jun 26 '17 at 14:01
  • @aircraft and neither did ps ... | grep ... which only managed to incorrectly match itself. – thrig Jun 26 '17 at 14:04

You must log in to answer this question.

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