How to Use SIGINT and Other Termination Signals in Linux
You must be used to use Ctrl + C for terminating any process, but do you know it sends the SIGINT
signal?
But there are more types of terminating signals than just using SIGINT
(cough Ctrl + c cough). So let's dive deep into the ocean of terminating signals!!
Use Termination Signals in Linux
The kill command is what users generally use for termination but do you know that 50+ signals kill command avails you?
This means you can use those numbers instead of writing the signal name.
For example, if you want to stop execution (suspending a running process), you're most likely to use SIGTSTP
like this:
kill -SIGTSTP %jobID
Similarly, you can use 20
as it is associated with SIGTSTP
to have the same effect:
kill -20 %jobID
But there are too many options so I'm going to cover widely used ones.
So let's start with the SIGSTP
.
Terminate processes using SIGTSTP
First, let me start with the killing foreground process.
It is quite simple compared to killing background processes, as you just have to use keybindings accordingly.
The keybinding of executing SIGTSTP
is ctrl + z
. For example, I'll be using the sleep command:
sleep 15000
Here you can see I've used the jobs command, a helpful utility that shows running and recently terminated processes.
But what about background processes? Well, there, you'd need to use the kill command specifying PID or JOB ID with -SIGTSTP
.
kill -SIGSTP %jobID
Use SIGCONT to resume command execution
There are times when you want to resume the command execution that you terminated recently and in those cases, -SIGCONT
will help you.
For this example, I'll be using the previously terminated process and then use the given command to resume its execution:
kill -SIGCONT %jobID
Terminate execution using SIGINT
This is my all-time favorite command, as whenever I find myself stuck somewhere, ctrl + c
makes wonders for me.
And if you're not aware of this, let me show you how it can terminate command executions with ease:
But the problem with SIGINT
is it may not work in some places such as bash interpreters.
For example, here's a bash script that takes input from the user:
#!/bin/bash
trap date SIGINT
read input
echo User input: $input
echo Exiting now
As you can see, it gave me the current date and time when I pressed ctrl + c
and only stopped when it got input from my side.
I know this script was created in that way but you got my point right. You can not rely on SIGINT
always.
Use SIGQUIT for termination
The SIGQUIT is similar to SIGINT but it also generates a core dump before execution.
In simple words, the core dump is a file that is generated automatically before the system crashes or the process is terminated.
Also, It can be provoked by using ctrl + \
. So you can use it for terminating foreground and background processes.
So let me show you how it behaves when you only try ctrl + \
:
But how to use it for terminating background processes? Let me bring light to the syntax part to make it easier:
kill -SIGQUIT %jobID
Terminate executions using SIGTERM
As its name suggests, this is a termination signal that terminates the program but unlike SIGKILL (that kills the program no matter what), this is a polite way of asking the program to be terminated.
If you want to learn more about SIGTERM vs SIGKILL, I'd recommend the other guide that touches on the core fundamentals:
One thing to remember is that sometimes it performs cleanups before proceeding with termination.
So the syntax of SIGTERM
is quite simple:
kill -SIGTERM %jobID
Use SIGKILL (the last resort)
Once in a while, I found myself in a situation where some programs won't work at all and they can't be terminated at all!
So in those cases, I use SIGKILL
to give that process the most unpleasant death.
Now, let's have a look at the generic syntax of SIGKILL:
kill -SIGKILL %jobID
Wrapping Up
This guide was a basic explanation of how you can use varieties of signal options to terminate executions. And if you still have any doubts, let me know in the comments.