When Does the System Send a SIGTERM to a Process?

In Linux, everything runs in the context of a process except the kernel. Each program is assigned a unique PID used by users and programs for communication. When a user prompts to kill a process using the kill command, it sends the specified signal to the targeted process. If a signal is not specified, SIGTERM is sent instead. Now you may wonder what SIGTERM is and what it sends to a process.

This guide explains everything about SIGTERM and what it sends to a process.

  • Process
  • Signal
  • SIGTERM
  • SIGKILL

What is a Process in Linux?

There are different definitions for a process, but the most prominent among them is “A process is a program which is being executed.” For example, when you open an application, a process is created for it by the operating system.

To view a list of current processes on a Linux system, use this command:

$ ps -aux

The “ps -aux” command displays a long list of the details about the currently running processes, and from here, the PID can also be viewed.

What is a Signal in Linux?

The signals in Linux act as a communicator for processors. This communication can be process-to-process or process-to-user. The signals work as the interrupts that tell a process that something important has happened. For example, the “Ctrl-C” is a signal for the terminal to stop the current process (you can try it yourself and notice that the currently executed command will no longer be executed.

What is SIGTERM in Linux?

Software Termination Signal or SIGTERM (Signal 15) provides a graceful way of terminating a process. By graceful, it means that it gives the time (a few seconds) to be prepared to shut down. It also allows the program to perform cleanup (temporarily created files) before being terminated.

When Does the System Send a SIGTERM to a Process?

The system sends a SIGTERM to a process when it is running and stops a process that is no longer needed or is misbehaving. When a SIGTERM signal is sent to a process, the operating system will notify it and give it a chance to clean up before terminating.

The SIGTERM can also be ignored by the programs (in extreme cases), and when the “kill” command is executed, OS sends a SIGTERM in the background by default.

To send a SIGTERM to a process, for example, the process 1877, use the kill command like this:

$ sudo kill 1877

Here, the process ID can be found using various commands, but the “ps command” is recommended. The SIGTERM signal is always sent before the SIGKILL signal.

What is SIGKILL in Linux?

Signal 9, or Signal Kill, is a signal in Linux which Linux OS uses to terminate the program instantly, and unlike SIGTERM, it cannot be ignored. The SIGKILL signal can cause system instability and could lead to data loss.

To immediately kill a process on Linux, for example, process 1905, use the kill command with “-9” to send a SIGKILL signal:

$ sudo kill -9 <Process-ID>

Conclusion

The “kill” command, when used without any specific signal, such as “-9” (stands for SIGKILL), sends a SIGTERM signal by default. This signal gives a soft warning to the program to shut down, and if it ignores the warning, the SIGKILL signal is sent(after a few seconds), causing immediate termination.

This guide has explained SIGTERM and when the system sends a SIGTERM to a process.