Posted on
Questions and Answers

Forward a trapped signal (eg, `SIGTERM`) to a child process without re-raising it

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Understanding Signal Forwarding in Bash: A Practical Guide

In the domain of Linux and Unix-like systems, understanding how to handle Unix signals is crucial for system administration and the development of robust shell scripts. One sophisticated yet practical task is forwarding a trapped signal to a child process. In this blog article, we will delve into some common questions and answers regarding this topic, explore basic examples, and provide a working script to demonstrate this process.


Q1: What is a Unix signal?

A1: A Unix signal is a limited form of inter-process communication used in Unix and Unix-like systems; it's a notification sent to a process in order to notify it of an event that occurred. Examples include SIGTERM (request to terminate) and SIGKILL (forceful termination).

Q2: Why might you need to forward a signal to a child process?

A2: Forwarding signals can be critical in situations where you need a parent script to handle specific terminations gracefully and ensure that any child processes also terminate correctly rather than continuing to run unexpectedly.

Q3: What does it mean to trap a signal in bash?

A3: To trap a signal in Bash is to define what action Bash should take when the script receives a specific signal. You can either perform a designated action, ignore the signal, or use the default signal handling behavior.

Q4: How can you forward a trapped signal to a child process in Bash?

A4: To forward a trapped signal, you first need to trap that signal in the parent bash script. Once trapped, you can redirect it to a child process using specific commands, typically involving the 'kill' command targeted at the child's process ID (PID).


Background and Basic Examples

Example of Signal Trapping:

Here is a simple example where a script traps the SIGTERM signal:

#!/bin/bash
trap 'echo "SIGTERM received, but not terminating."' SIGTERM
echo "My PID is $$"
while true; do
  sleep 1
done

In this script, SIGTERM is trapped and instead of terminating, it prints a message and continues running.

Forwarding a Signal:

Here's a theoretical snippet to forward SIGTERM to a child:

#!/bin/bash
trap 'kill -TERM $child_pid' SIGTERM

# Start the child process
./child_script &
child_pid=$!

# Wait for the child process to finish
wait $child_pid

Here, a SIGTERM received by the parent is forwarded to the child by sending SIGTERM to the child’s process ID.

Demonstration Script

Let’s create a working example where a parent script spawns a child process, traps SIGTERM, and forwards it to the child.

#!/bin/bash
# Parent Script

echo "Parent script PID: $$"

# Function to handle SIGTERM
handle_sigterm() {
    echo "Parent received SIGTERM, forwarding to child PID: $child_pid"
    kill -TERM $child_pid
}

# Trap SIGTERM and call handle_sigterm when received
trap handle_sigterm SIGTERM

# Start child script in background
./child_script.sh &
child_pid=$!

# Wait for child process to finish
wait $child_pid
exit 0
#!/bin/bash
# child_script.sh
echo "Child script running, PID: $$"
while true; do
  sleep 1
done

Usage: Run the parent script, note down its and the child's PID. From another terminal, send SIGTERM to the parent:

kill -TERM <parent_pid>

You will see that the child receives the forwarded SIGTERM signal from the parent.

Summary and Conclusion

Understanding and managing signals, particularly in scripts that involve parent-child processes, is crucial for building stable system applications. By effectively using signal trapping and forwarding, we can enhance the control over how processes react to termination requests and other events, ensuring a graceful termination or proper resources cleanup. This foundational technique is a building block for more complex process management in Bash scripts and system administration tasks in Linux.

Further Reading

To further explore Unix signals and signal handling in Bash, consider the following resources:

  • Unix Signals and Bash: GNU.org Bash Reference Manual This section of the Bash Reference Manual explains how Bash interacts with signals and how these can be trapped and handled.

  • Advanced Bash-Scripting Guide: Signal Handling Offers a detailed overview of how to handle signals within bash scripts, including examples and explanations of different signal types.

  • Linux Signal Handling: IBM Developer Articles Detailed guide about signal handling on Linux systems, covering essential commands and strategies.

  • Understanding Linux Processes: Digital Ocean Explains process management in Linux, including how signals affect processes and how they can communicate.

  • Practical Usage of Signals: Linux Programming Interfaces - Michael Kerrisk Focuses on programming interfaces under Linux, providing advanced insights into handling signals programmatically within system applications.

These resources provide a comprehensive view on handling Unix signals in scripting and programming environments, complementing the knowledge from the initial article.