DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

Handling System Signals in Ruby: Graceful Shutdowns & Process Management

February 24, 2025

In the world of backend development, managing system signals is crucial for ensuring that applications handle shutdowns and restarts smoothly. In Ruby, the Signal module provides an elegant way to trap and respond to system signals, making it a powerful tool for process control.


Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>


Need Expert Ruby on Rails Developers to Elevate Your Project?


📌 What Are System Signals?

System signals are notifications sent to processes by the operating system or other processes. Some common signals include:

  • SIGINT (Ctrl+C): Interrupt signal, typically used to stop a running process.
  • SIGTERM : Termination signal, often sent when stopping a server or container.
  • SIGHUP : Hang-up signal, used to reload configurations in many daemons.
  • SIGUSR1 / SIGUSR2 : Custom user-defined signals, often used in web servers.

🛠 Handling Signals in Ruby

Ruby allows you to trap these signals and execute custom logic before a process exits.

1⃣ Graceful Shutdown for Ruby Applications

When running a background job, web server, or daemon, it’s essential to handle shutdown signals properly. Here’s how you can catch SIGTERM and clean up before exiting:

Signal.trap("TERM") do
  puts "Received SIGTERM. Shutting down gracefully..."
  # Perform cleanup tasks (e.g., closing DB connections)
  exit
end
Enter fullscreen mode Exit fullscreen mode

This ensures that resources are released properly, avoiding issues like unfinished database transactions or orphaned processes.

2⃣ Preventing Abrupt Interruptions (SIGINT)

When working in a terminal, pressing Ctrl+C sends SIGINT. You can override this behavior to clean up before exiting:

Signal.trap("INT") do
  puts "Caught SIGINT! Performing cleanup..."
  exit
end
Enter fullscreen mode Exit fullscreen mode

This is useful when running scripts that shouldn’t terminate immediately upon interruption.

3⃣ Reloading Configuration Without Restarting (SIGHUP)

Many applications use SIGHUP to reload their configuration without a full restart. Here’s how you can implement it in Ruby:

Signal.trap("HUP") do
  puts "Reloading configuration..."
  # Code to reload settings or restart components
end
Enter fullscreen mode Exit fullscreen mode

This is particularly useful in long-running services where downtime must be minimized.

🚀 Signal Handling in Rails & Background Jobs

Many Ruby-based frameworks and tools rely on signals:

✅ Puma & Unicorn use SIGTERM for graceful shutdowns and SIGUSR2 for hot restarts. ✅ Sidekiq listens for SIGTSTP and SIGTERM to stop processing jobs cleanly. ✅ Docker & Kubernetes send SIGTERM when stopping containers, so handling this in Ruby apps ensures proper shutdown.

🔥 Final Thoughts

Understanding and using Signal in Ruby allows developers to build more resilient, maintainable, and production-ready applications. Whether you’re handling SIGTERM in a web server, catching SIGINT in a script, or reloading configs with SIGHUP, mastering signals is a valuable skill.

💡 Have you used signal handling in your Ruby projects? Let’s discuss in the comments! 🚀

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or leave a kind comment!

Okay