What's the difference between the SIGINT
signal and the SIGTERM
signal? I know that SIGINT
is equivalent to pressing Ctrl+C on the keyboard, but what is SIGTERM
for? If I wanted to stop some background process gracefully, which of these should I use?
3 回答
The only difference in the response is up to the developer. If the developer wants the application to respond to SIGTERM
differently than to SIGINT
, then different handlers will be registered. If you want to stop a background process gracefully, you would typically send SIGTERM
. If you are developing an application, you should respond to SIGTERM
by exiting gracefully. SIGINT
is often handled the same way, but not always. For example, it is often convenient to respond to SIGINT
by reporting status or partial computation. This makes it easy for the user running the application on a terminal to get partial results, but slightly more difficult to terminate the program since it generally requires the user to open another shell and send a SIGTERM
via kill
. In other words, it depends on the application but the convention is to respond to SIGTERM
by shutting down gracefully, the default action for both signals is termination, and most applications respond to SIGINT
by stopping gracefully.
If I wanted to stop some background process gracefully, which of these should I use?
The unix list of signals date back to the time when computers had serial terminals and modems, which is where the concept of a controlling terminal originates. When a modem drops the carrier, the line is hung up.
SIGHUP(1) therefore would indicate a loss of connection, forcing programs to exit or restart. For daemons like syslogd and sshd, processes without a terminal connection that are supposed to keep running, SIGHUP is typically the signal used to restart or reset.
SIGINT(2) and SIGQUIT(3) are literally "interrupt" or "quit", "from keyboard", giving the user immediate control if a program would go haywire. With a physical character based terminal this would be the only way to stop a program!
SIGTERM(15) is not related to any terminal handling, and can only be sent from another process. This would be the conventional signal to send to a background process.
SIGINT is a program interrupt signal, which will sent when an user presses Ctrl+C. SIGTERM is a termination signal, this will sent to an process to request that process termination, but it can be caught or ignored by that specific process.