Linux kill signals are a crucial mechanism for process communication and management. These signals provide a way to send specific instructions to processes, with each signal representing a different type of communication or action.
Signal | Name | Description | Default Action |
---|---|---|---|
SIGTERM (15) | Terminate | Graceful shutdown request | Terminate process |
SIGKILL (9) | Kill | Forceful termination | Immediately stop process |
SIGINT (2) | Interrupt | Interrupt from keyboard (Ctrl+C) | Terminate process |
SIGHUP (1) | Hangup | Reload configuration or terminate | Terminate process |
In containerized environments, proper handling of kill signals is critical to ensure:
- Graceful shutdown of worker processes
- Completion of in-progress jobs
- Proper resource cleanup
- Minimal service disruption
Sidekiq provides robust signal handling for graceful shutdowns:
# Example Sidekiq signal handling
Sidekiq.configure_server do |config|
config.on(:shutdown) do
# Perform cleanup operations
puts "Gracefully shutting down Sidekiq"
end
end
Laravel workers can implement graceful shutdown mechanisms:
// Laravel worker signal handling
public function handle()
{
// Register signal handlers
pcntl_signal(SIGTERM, function () {
$this->shouldStop = true;
});
while (!$this->shouldStop) {
// Process jobs
$this->processNextJob();
}
}
Grace periods are crucial for ensuring uninterrupted job processing:
services:
worker:
stop_grace_period: 30s # 30-second grace period
stop_signal: SIGTERM # Use graceful termination
apiVersion: apps/v1
kind: Deployment
metadata:
name: worker-deployment
spec:
template:
spec:
containers:
- name: worker
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 30"]
Supervisord provides a robust solution for managing long-running processes and handling signal propagation in containerized environments.
When a container receives a termination signal, the process flow typically looks like this:
Container Termination Signal (SIGTERM)
│
↓
Supervisord
│
↓
Supervised Processes
│
↓
Application Graceful Shutdown
[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
[program:worker]
command=/usr/bin/php /app/artisan queue:work
autostart=true
autorestart=true
stopwaitsecs=30
stopsignal=SIGTERM
stopasgroup=true
killasgroup=true
[program:sidekiq-worker]
command=bundle exec sidekiq
autostart=true
autorestart=true
stopwaitsecs=30
stopsignal=SIGTERM
stopasgroup=true
killasgroup=true
services:
app:
build: .
volumes:
- ./supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
stop_signal: SIGTERM
stop_grace_period: 45s
#!/bin/bash
# Trap SIGTERM signal
trap_sigterm() {
echo "Received SIGTERM. Initiating graceful shutdown..."
# Notify Supervisord to stop workers
supervisorctl stop all
# Wait for processes to shut down
wait_for_workers_shutdown
# Perform any additional cleanup
cleanup_resources
exit 0
}
wait_for_workers_shutdown() {
timeout=30
while [ $timeout -gt 0 ]; do
active_workers=$(supervisorctl status | grep -E "RUNNING|STARTING" | wc -l)
if [ $active_workers -eq 0 ]; then
echo "All workers have shutdown successfully"
return 0
fi
sleep 1
((timeout--))
done
echo "Some workers did not shutdown in time"
return 1
}
cleanup_resources() {
# Example: Clear temporary files, close database connections
rm -rf /tmp/worker-*
echo "Cleanup complete"
}
# Attach the trap
trap trap_sigterm SIGTERM
# Start Supervisord
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
- Parent Process Responsibility: Supervisord acts as a parent process managing child processes
- Signal Forwarding: Uses
stopasgroup=true
andkillasgroup=true
to ensure signal propagation - Graceful Termination Sequence:
- Receive SIGTERM
- Notify all child processes
- Wait for processes to complete
- Force terminate if grace period expires
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deployment
spec:
template:
spec:
containers:
- name: app
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "supervisorctl stop all"]
- Zombie Processes: Use
init
systems or containers with proper PID 1 management - Incomplete Shutdown: Implement robust timeout mechanisms
- Resource Leaks: Always include cleanup scripts
- Use
stopsignal=SIGTERM
in Supervisord - Implement proper timeout mechanisms
- Log all shutdown and cleanup activities
- Test signal handling thoroughly in staging environments
Effective signal management with Supervisord provides a robust mechanism for graceful process termination, ensuring minimal disruption and proper resource management in containerized environments.