Why can ps know about the process that runs after it?

2

[root@myvm1 ~]# ps aux | grep ls
root     13594  0.0  0.1   3912   672 pts/1    R+   09:46   0:00 grep ls

In the above, the ps aux runs first and its output is then redirected to the grep ls command. The grep ls command runs after ps aux.

So why can ps know about the process that runs after it and has it in its output?

bobo

Posted 2014-01-14T10:24:24.553

Reputation: 1 649

2While grep can't process any output until after it's created, the grep process can be created either before or after the ps process. There's no special reason it has to be one way or the other. – David Schwartz – 2014-01-14T10:35:46.387

Answers

4

In the above, the ps aux runs first and its output is then redirected to the grep ls command. The grep ls command runs after ps aux.

So why can ps know about the process that runs after it and has it in its output?

You are writing about a logical order of command sequencing.

If you get insights in bash proceedings you get to know that both commands and the piping framework may be created in an order that does not really matter.

Moreover the piping framework is most probably a command output redirection.

Your answer is: the command ps is run actually after the process that will run grep has been created.

174140

Posted 2014-01-14T10:24:24.553

Reputation: 859

Thanks. Based on your answer, I have find another answer that explains this in more detail: http://unix.stackexchange.com/questions/37508/in-what-order-do-piped-commands-run

– bobo – 2014-01-14T10:53:10.217