
Ps -ef | grep dbus | grep -v grep | awk '’. We’ll use the following script, pipe_order.sh: #!/bin/bash Having seen that the piped processes run concurrently, we’ll discuss in which order the operating system spawns the piped processes. Therefore, we see that both commands in the pipeline are running concurrently using the ps command. The other grep command running was the second part of the pipeline ps -ef | grep grep. That means the operating system spawned it just after running the tail -f /dev/null command with PID 12419. The first grep in the output is the second command in the pipeline, grep Hello. This is the first command in the pipeline. There’s a tail command running as expected. On another terminal, let’s check whether there’s a tail command running: $ ps -ef | grep tail | grep -v grepĪlice 12419 20206 0 06:53 pts/5 00:00:00 tail –f /dev/null Of course, nothing will be appended to /dev/null as it’s a null device file. In this example, we ran the tail -f /dev/null command to follow the new logs appended to the file /dev/null, and we searched for the word Hello in the logs using grep Hello. Therefore, both processes seem to run concurrently.Īs an additional example that proves the concurrent operation of piped processes, we’ll use another pipeline: $ tail -f /dev/null | grep Hello If the operating system had spawned the process corresponding to grep tail after the process of ps –ef had ended, we wouldn’t have observed it in the output. However, the second command in the pipeline, grep tail, was listed in the output. There’s no tail command running, according to the output.

The order isn’t guaranteed and may change from one execution to another. The execution order of the commands depends on the scheduling policy of the operating system. In fact, the operating system may not run the commands in a pipeline starting from left to right. Therefore, the processes run concurrently. The shell starts the second process while the first process is still running. We may think that the first process runs and exits, then its buffered output is fed to the second process. Sometimes, there might be a misunderstanding about the lifetimes of the processes in a pipeline.
:max_bytes(150000):strip_icc()/linux-dot-character-5376c26eb47c4c8fbe08b42f18f9d0c4.jpg)
Similarly, it directs the standard output of the second process spawned to the standard input of the third process spawned by running grep -v grep. Here, the shell directs the standard output of the first process spawned by running ps -ef to the standard input of the second process spawned by running grep tail. Let’s consider an example pipeline: $ ps -ef | grep tail | grep -v grep In this case, the shell is the ancestor of each process in the pipeline. The operating system directs the standard output of each process in the pipeline to the standard input of the next process in the pipeline. When we connect several commands with pipes in a shell, we create a pipeline.
