Why can't Unix programs have signals with meaningful program defined names (rather than USR1, etc)?

Nathan Long picture Nathan Long · Mar 18, 2011 · Viewed 54.2k times · Source

Many Unix programs accept signals like USR1 and USR2. For example, to upgrade the executable for Nginx on the fly, you send kill -USR2.

I understand that USR1 is a "user defined" signal, meaning that whoever created the program can use it to mean "shut down" or "dump your logs" or "print foo a thousand times" or whatever. But I don't understand why they must use this arbitrary name. Why not kill -UPGRADE, or kill -GRACEFUL_SHUTDOWN? Does Unix only allow specific signals?

While we're at it, Nginx also uses the following signals (see documentation):

  • TERM, INT: Quick shutdown
  • QUIT: Graceful shutdown
  • HUP:
    • Configuration reload
    • Start the new worker processes with a new configuration
    • Gracefully shutdown the old worker processes
  • USR1: Reopen the log files
  • USR2: Upgrade Executable on the fly
  • WINCH: Gracefully shutdown the worker processes

HUP? WINCH? What's the reason for these names? Where can I learn more about this?

Answer

Erik picture Erik · Mar 18, 2011

The signals available on an OS are defined by the OS (usually following POSIX) - they're not "strings" but rather integer constants with standard names. USR1 and USR2 are the two signals that have no attached specific meaning - intended for whatever arbitrary use the developer wants.

On your linux machine, read man 7 signal for an overview of signal handling and signals.

You can redefine the meaning of other signals if you're prepared to deal with the OS issuing those signals in response to events. You can e.g. make HUP mean "reload configuration" - as long as you're either certain that the process will never get a hangup (terminal loss), or you're prepared to handle cases where the OS and not a user sends the HUP signal.