0

I run ps aux | grep somethinghere.

The output shows the grep somethinghere as a running process.

My question is, shouldn't ps aux finish first, then grep somethinghere run on its output? Maybe there is no need for ps to finish (there is a pipe between them), but it should run as the first process, while grep isn't running.

The output means that grep is ran before ps!

How is this? Shouldn't ps run first, because its output should be piped to grep? Even if they are ran at the same time, why is that I always see grep in output? Shouldn't I not see grep sometimes, too?

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
vfsoraki
  • 145
  • 7

2 Answers2

1

You should look at this page.


Edit, now that I understand what you are asking:


Maybe this will better help explain.

The order the commands are run actually doesn't matter and isn't guaranteed. Leaving aside the arcane details of pipe(), fork(), dup() and execve(), the shell first creates the pipe, the conduit for the data that will flow between the processes, and then creates the processes with the ends of the pipe connected to them. The first process that is run may block waiting for input from the second process, or block waiting for the second process to start reading data from the pipe. These waits can be arbitrarily long and don't matter. Whichever order the processes are run, the data eventually gets transferred and everything works.

devnull
  • 5,331
  • 21
  • 36
  • Good link, thanks. But it doesn't mention the execution order, anyway – vfsoraki Jan 30 '15 at 19:00
  • "So the only output you see is from grep." What relation does that have to the question? – Hauke Laging Jan 30 '15 at 19:02
  • @HaukeLaging What do you mean what relation does this have? `The output means that grep is ran before ps!` I believe OP is confused as to why despite `ps` running first you don't see the complete output first then grep's. I am trying to clarify that `ps` does run first but because the output is piped to grep that the only thing displayed is the filtered result. – devnull Jan 30 '15 at 19:07
  • @DevNull No I didn't mean this. I know at last I see `grep`'s output, but the question is why the process `grep` is included in the output of `ps`? Shouldn't `grep` run after `ps`? – vfsoraki Jan 30 '15 at 19:09
  • @thelastblack Oh, that makes more sense now. I believe the answer to that is because before `ps` can execute, 'pipe' waits `ps` until `grep` is ready for the output of `ps`, because of this, `grep` is actually running when `ps` executes therefore it is in the results. – devnull Jan 30 '15 at 19:12
  • That the output comes from `grep` doesn't say anything about the problem. – Hauke Laging Jan 30 '15 at 19:24
  • “`ps aux` runs first” is wrong (as the text you quote explains). The two sides of the pipe run in parallel. Which program is started first is unpredictable. – Gilles 'SO- stop being evil' Jan 31 '15 at 22:45
  • @Gilles you are 100% correct. At first I didn't understand what OP was asking. I removed the incorrect statements. Thanks. – devnull Jan 31 '15 at 22:57
0

I suppose that OP mean "How to strip grep somethinghere process from ps output list of somethinghere prosesses". There are dozen way to do it:

  1. If you need just number you can use pgrep -l somethinghere
  2. Double grep ps aux | grep somethinghere | grep -v grep (have met ofen but I do not like it)
  3. Make some regexp with somethinghere ps aux | grep [s]omethinghere
Costas
  • 14,806
  • 20
  • 36
  • I didn't mean this. I meant why is `grep` showing in `ps` output, while it should run after `ps`? – vfsoraki Jan 30 '15 at 18:53
  • @thelastblack Now I see. The "pipe" do not mean that the second command run after first ones just redirect output of the first command to input of the second. More over, I think to be able to provide the action shell have to start second command fist to make its input to be ready to receive information from the first command. To avoid this you can use "named pipes" (info is easy can be found here on the site) – Costas Jan 30 '15 at 20:10