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.
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 python myscript.py
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
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.
this signal is similar to
SIGINTbut 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
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.
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.
SIGSTOP signal stops the running process, you can send a
SIGSTOP signal by using the keyboard shortcut
SIGCONT signal continues a running process. You can resume a stopped process by running the command
%1is the program/job that is located on the first item of the jobs list.
> jobs  + suspended sleep 1000  - running nohup sleep 2000
bgsends commands to the program running on the background, if we want to continue a program in the current terminal we can use
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>
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.