Understanding Linux Signals

I'm The Flying Dev - flight attendant and self-taught developer. The founder and host of the Landing in Tech podcast.
Search for a command to run...

I'm The Flying Dev - flight attendant and self-taught developer. The founder and host of the Landing in Tech podcast.
No comments yet. Be the first to comment.
If you use an email long enough, you know how much spam that email will get - some even pass through the filter and reach your inbox. When this happens, you can do three things: Add filtering to delete emails from a specific domain/email address aut...

Actions I've taken to improve security and accounts hardening

When I built the website for Landing in Tech I wanted to have a bar on top of the page with the latest episode. The idea was to allow users to listen to the episode while browsing the page. I added the iframe on my layout.js file and expected it to ...

Version control systems are tools to keep track of changes to source code or other collections of files and folders. They help to track the changes of documents, they make it easy collaborating with someone else. Version Control track changes in a se...

As developers, we use the terminal all the time, it makes sense to learn some shortcuts that can help you do things in the command line. Learning these will also help you boost your productivity because you don't have to retype your commands again j...

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.
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.
SIGHUP SignalThis 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
SIGINT SignalThis 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.
SIGQUIT Signalthis 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 CTRL+\
SIGKILL SignalThese 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.
SIGTERM SignalThis 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 and SIGCONT SignalsThe 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:
%1is 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:
bgsends commands to the program running on the background, if we want to continue a program in the current terminal we can usefginstead
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: