|
Shell provides several ways to spawn and control processes. This covers subshells, command execution, and process management. This example shows how to run one command based on the output of another command. |
|
|
Output:
/tmp/hello.txt /tmp/hello_world.txt /tmp/hello_world_2.txt |
|
|
|
|
|
Output:
Nested: local |
|
|
Commands in a subshell with |
|
|
Output:
Subshell demo: In subshell: /tmp After subshell: /root x is: unset |
|
|
Command groups with Variables defined in a command group are visible to the parent shell. |
|
|
Output:
Command group demo: In group y is: group_value |
|
|
If executed in a subshell, any remaining code in the subshell will not run. |
|
|
Output:
Before exec This replaces the subshell After subshell |
|
|
Bash
Only |
|
|
Output:
Script PID: 7 Script BASHPID: 7 Command group PID: 7 Command group BASHPID: 7 Subshell PID: 7 Subshell BASHPID: 8 |
|
|
|
|
|
Output:
Background PID: 8 |
|
|
Bash
Pipes connect stdout to stdin. Note that this example is
bash-specific due to the use of |
|
|
Output:
Pipeline: apple banana |
|
|
Pipeline exit status is the last command’s status: |
|
|
Output:
Pipeline exit: 1 |
|
|
Bash
Bash provides a |
|
|
Output:
PIPESTATUS: 0 1 0 |
|
|
Execute command from string.
Be careful with |
|
|
Output:
Hello from eval |
|
|
|
|
|
Output:
Processing file1 Processing file2 Processing file3 |
|
|
Note that when processing in parallel, the order of the output is not guaranteed. |
|
|
Output:
Processing file1 Processing file2 Processing file3 |
|
|
Background jobs let you run commands asynchronously. This is shell’s simple form of concurrency. Run a command in background with |
|
|
Output:
Starting background job... Background job started with PID: 7 |
|
|
Wait for specific process can be done with The syntax is:
wait If the process is finished, the exit status is 0. Otherwise, the exit status is 1. |
|
|
Output:
Starting background job... Background job started with PID: 8 Waiting for job 8... Job finished with status: 0 |
|
|
If no PID is provided, |
|
|
Output:
Starting multiple background jobs: PIDs: 8, 9, 10 Waiting for all jobs... All jobs finished |
|
|
|
|
|
Output:
Process 8 is running |
|
|
The Note that running |
|
|
Output:
Running jobs: 3 |
|
|
Subshells also have their own PIDs that can be waited for. |
|
|
Output:
Waiting for process 7... Process 1 done Process 7 finished Waiting for all, including process 8... Process 2 done |
|
|
Bash
Disowned job will keep running after shell exits. For job control in interactive shells:
|
|
|
|
|
|
This example shows how to run multiple tasks in parallel. The |
|
|
Output:
Parallel execution: Task 1 complete Task 2 complete Task 3 complete All tasks done |
|
|
Bash
Limit concurrent jobs can be done with The syntax is: wait -n If the job is finished, the exit status is 0. Otherwise, the exit status is 1. This example shows how to limit concurrent jobs. |
|
|
Output:
Limited concurrency: Job 1 done Job 2 done Job 3 done Job 4 done Job 5 done |
|
|
Subshells also have their own traps that can be used to clean up resources. |
|
|
Output:
Started 3 background jobs The pids will be printed out on exit In the normal case, one would kill the pids instead 11 10 9 Subshell finished |
|
|
Bash
Process substitution in bash can be performed via <(command) or >(command). This is usually done in order to avoid creating temporary files. |
|
|
Output:
Line: test.txt Line: test2.txt |
|
|
Named pipes can be used for inter-process communication. The |
|
|
Output:
Named pipe communication: Received: Hello from producer |
|
|
This example shows how to use a named pipe to implement the producer-consumer pattern more generally. The same patter used in the previous example is used here. |
|
|
Output:
Consumed: item1 Consumed: item2 Consumed: item3 |
|
|
An alternative to a named pipe is to use a temp file. A named pipe is a more efficient way to communicate between processes as the data does not get written to disk and data is consumed when read, while a temporary file persists past the end of the script (if not cleaned up) and can have it’s data read by multiple processes. |
|
|
Output:
Doing other work... Still working... Got result: Background result |
|
|
You can also use a pipeline to run commands in background. This example adds a newline to the end of the pipeline to ensure that the last line is processed. |
|
|
Output:
Background in pipeline: Processing: one Processing: two |
|
|
Bash
In bash, you can use The |
|
|
Output:
Result: 5 |
|