Understanding Linux Signals

Understanding Linux Signals

Signals provide a method for the user to communicate directly with a process, they indicate that an important event has occurred and something should happen.

For example, when you are running a program and you use CTRL+C to stop the program, you are sending a signal that tells the process that should interrupt its execution.

Why should you care about signals

You might be wondering, what's so important about this concept. It's because they are something that you rely on under the hood while using the command line. You can also write your code to handle the different signals and make your program do something else.

Understanding signals will also help you deal with programs that don't quit with CTRL+C and the only thing that seems to help is running the command kill <pid> to stop a process.

Exiting, Stopping and Continuing Signals

Let's have a look at signals that stops or terminates a process. Understanding these signals can be useful when your process just seems to freeze and nothing seems to be working when you try to stop it.

The SIGHUP Signal

This signal stands short for hang up, this signal is usually triggered when you have processes running in the terminal and you decide to close the terminal.

When you have an ssh session open and a running process, if you exit the session that could trigger a SIGHUP signal, if you want to make sure your process keeps running even if you close the terminal window, you can run its command with the nohup prefix.

For example:

nohup python myscript.py

The SIGINT Signal

This signal stands short for Interrupt, this signal is usually triggered when you want to interrupt a process. The keyboard shortcut to send this signal is by using CTRL+C.

This signal doesn't always stop a running process. This can be for various reason, maybe the process has a way to handle this signal, the process is busy or you are dealing with async threads.

The SIGQUIT Signal

this signal is similar to SIGINT but produces a core dump when it terminates the process, just like a program error signal. You can think of this as a program error condition “detected” by the user. GNU - Termination Signals

The Keyboard shortcut to send this signal is by using CTRL+\

The SIGKILL Signal

These signal forces terminate the process and it cannot be intercepted, ignored, blocked or handled. This signal is usually generated only by request and should be used as a last resort.

The SIGTERM Signal

This signal requests a process to terminate. Unlike SIGKILL this signal can be caught, ignore or interpreted. SIGTERM allows the process to terminate gracefully and release resources.

The shell command kill generates a SIGTERM by default.

The SIGSTOP and SIGCONT Signals

The SIGSTOP signal stops the running process, you can send a SIGSTOP signal by using the keyboard shortcut CTRL+Z.

The SIGCONT signal continues a running process. You can resume a stopped process by running the command bg %1

Note: %1 is the program/job that is located on the first item of the jobs list.

> jobs
[1] + suspended sleep 1000
[2] - running nohup sleep 2000

NOTE: bg sends commands to the program running on the background, if we want to continue a program in the current terminal we can use fg instead

Using Signals

This might be a bit counter-intuitive, but you can use the kill command to send a signal to a process, if you use a flag with this command.

For example, if you want to send a SIGHUP signal, you can run the command

kill -HUP <process id>

Each signal has an alphanumeric value that can be used as a flag with the kill command. Let's take the previous example, if we want to send a SIGHUP signal again you can run the command

kill -1 <process id>

You can check the value of each signal on the signal man pages or the C++ Signals Tutorial.

Hope this helps you understand what are signals how you can use them on your day-to-day. I've learned about this subject on the lesson 5 of Missing Semester.


References: