Tag Info

New answers tagged

3

If you press c while in top then the command will be expanded to show the full command used to start the process. You can also take the PID and run: ps -ef | grep $PID Or: cat /proc/$PID/cmdline


2

Consider this thought experiment: if ps ran to completion before invoking grep as you think it should, that would mean that the shell would need to: Invoke ps. Buffer all of its output (because it has nowhere else to go yet). Invoke grep. Feed the saved output to grep's stdin. This is wasteful; it's a more efficient use of memory to invoke grep first, ...


1

Just confirming as BowlOfRed and alexises stated. Here is a visual representation piping ps and grep back to ps again to show how the piped processes run. Note that the final ps x --forest is ignoring the input, but it keeps the previously piped processes alive so they will be displayed. ps -ef | grep myprocess | ps x --forest Excerpt of output: 25056 ...


6

When I'm only interested in presence of a process, I use pgrep which doesn't show this behaviour, e.g.: $ pgrep myprocess 1900 In other cases (when i'm interested in more info), I usually add a '| grep -v grep' to discard grep lines, e.g.: $ ps -ef | grep myprocess| grep -v grep hth.


10

This behavior is totally normal, it's due to how bash manages pipe usage. pipe is implemented by bash using the pipe syscall. After that call, bash forks and replaces the standard input (file descriptor 0) with the input from the right process (grep). The main bash process creates another fork and passes the output descriptor of the fifo in place of the ...


6

The pipe does not behave like ;. It starts both the processes together. Which is why the grep command showed up too. So when you gave ps aux | grep myprocess , the ps aux included the grep myprocess, so the grep included that in its output. To check this, I gave two dd commands on my test server like this: [sreeraj@server ~]$ dd if=/dev/urandom ...


3

You're correct, that's not how it works. The shell doesn't wait for the first process in a pipeline to exit before starting the next. It starts them all. As such, the ps may see the grep already running.



Top 50 recent answers are included