Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Shell Scripting – How to Send Signal to a Process?
In this tutorial, we will explore how to send signals to processes using shell scripting. Signals are software interrupts that enable communication between processes, allowing the operating system and applications to notify processes about events or request specific actions. Understanding signal handling is essential for effective process management in Unix-like systems.
Signals serve various purposes including graceful process termination, handling interrupts, suspending processes, and modifying process behavior. Shell scripts can leverage these signals to control running processes programmatically, making system administration and automation more efficient.
Understanding Signals and Their Usage
Signals are software interrupts sent to processes to inform them about specific events or prompt particular actions. Common signals include SIGTERM (graceful termination), SIGKILL (forceful termination), SIGSTOP (suspend process), and SIGCONT (resume process).
The primary command for sending signals is kill, which despite its name, can send any signal to a process. By default, kill sends the SIGTERM signal, requesting graceful termination.
#!/bin/bash # Send SIGTERM signal to a specific process kill <process_id> # Send a specific signal (SIGKILL) to a process kill -SIGKILL <process_id> # Alternative numeric format kill -9 <process_id>
To find process IDs, use commands like ps, pgrep, or pidof to identify target processes accurately.
Common Signal Types
| Signal | Number | Description | Can be Caught/Ignored |
|---|---|---|---|
| SIGTERM | 15 | Graceful termination request | Yes |
| SIGKILL | 9 | Forceful termination | No |
| SIGSTOP | 19 | Suspend process | No |
| SIGCONT | 18 | Resume suspended process | Yes |
| SIGHUP | 1 | Hangup (reload configuration) | Yes |
Sending Signals to Specific Processes
The pkill command allows sending signals to processes based on names or other criteria, eliminating the need to find process IDs manually.
#!/bin/bash # Send SIGTERM to processes by name pkill -SIGTERM firefox # Send SIGKILL to processes matching pattern pkill -9 "chrome.*" # Send signal to processes owned by specific user pkill -SIGUSR1 -u username process_name
The killall command provides similar functionality, sending signals to all processes with a specified name.
#!/bin/bash # Terminate all instances of a program killall -SIGTERM apache2 # Force kill all matching processes killall -9 unresponsive_app
Sending Signals to Process Groups
Process groups contain related processes spawned by a common parent. To send signals to entire process groups, use a negative process group ID with the kill command.
#!/bin/bash # Send SIGTERM to entire process group kill -SIGTERM -<process_group_id> # Example: Send SIGKILL to process group 1234 kill -SIGKILL -1234 # Send signal to current process group kill -SIGTERM 0
Use ps -o pid,pgid,comm to identify process group IDs. The negative sign indicates targeting the entire group rather than an individual process.
Sending Signals to Remote Processes
SSH enables sending signals to processes on remote machines by executing commands remotely. This approach requires proper authentication and network connectivity.
#!/bin/bash # Send signal to remote process ssh user@remote_host "kill -SIGTERM 1234" # Send signal to remote process by name ssh user@remote_host "pkill -SIGUSR1 nginx" # Check if process exists before sending signal ssh user@remote_host "pgrep mysql && kill -SIGHUP \$(pgrep mysql)"
For automated scripts, configure SSH key-based authentication to avoid password prompts. Always verify process existence before sending signals to prevent errors.
Practical Examples
Graceful Service Restart
#!/bin/bash
SERVICE_NAME="nginx"
# Check if service is running
if pgrep "$SERVICE_NAME" > /dev/null; then
echo "Restarting $SERVICE_NAME gracefully..."
pkill -SIGHUP "$SERVICE_NAME"
echo "Restart signal sent"
else
echo "$SERVICE_NAME is not running"
fi
Process Monitoring and Cleanup
#!/bin/bash # Clean up zombie processes kill -SIGCHLD 1 # Suspend resource-heavy processes pkill -SIGSTOP heavy_process # Resume processes after system maintenance sleep 300 pkill -SIGCONT heavy_process
Conclusion
Signal handling in shell scripting provides powerful process control capabilities. The kill, pkill, and killall commands enable precise targeting of individual processes, groups, or remote processes. Understanding different signal types and their behaviors is crucial for effective system administration and process management automation.
