Course contentsShow
← Linux
Lesson 257 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Process Group Signaling

Coordinate large sets of processes using shell signals.

In this lesson, you will learn how to use process group signaling to send a single command to an entire family of related processes simultaneously.

Understanding Process Groups

When you run a command in your terminal, the shell often creates a "Process Group." This is essentially a bucket that holds the main command and any child processes it spawns (like pipes or subshells). Instead of hunting down every single process ID (PID) to stop or pause them, Linux allows you to target the entire group at once.

The Power of the Negative PID

In our previous lessons on signal handling, we learned that sending a signal to a specific PID affects only one process. However, when we use the kill command, we can pass a negative PID to target a process group.

When you send a signal to a process ID written as -PID, the shell interprets this as: "Send this signal to every process that shares this process group ID."

Consider a scenario where you have a complex pipeline:

# Start a long-running, multi-process task
cat /dev/zero | gzip | ssh remote "cat > backup.gz" &

In this command, the shell creates a process group for all three programs. If you want to stop the entire operation, you don't need to find each PID individually. If the "process group leader" (the first command) has a PID of 1234, you can terminate everything in that group with one command:

kill -TERM -1234

Why This Matters

If you kill only the first process, the children (gzip and ssh) might become "orphaned" or keep running in the background, consuming your CPU and network. By using process group signaling, you ensure that the entire tree of processes receives the signal at the exact same time, maintaining control over your system resources and preventing "zombie" processes from lingering. This is the mechanism the shell uses internally whenever you press Ctrl+C in your terminal—it sends a SIGINT to the entire foreground process group.

Key Takeaway: By using a negative PID with the kill command, you can send signals to an entire group of related processes at once, ensuring clean and synchronized termination.