Terminating a Process in Bash
This article will first discuss the different concepts related to Linux processes. After this, we will learn the different ways to terminate a process.
Before going into the kill command, we must understand some preliminary concepts.
Simple procedures and Bash procedures
An active or running program in an operating system is called a process. For example, when we run an editor or a browser, the operating system creates a process for each.
On the other hand, a simple executable file that is run in the system through Bash is called a Bash process. For example, whenever we run any application like calculator through Bash shell, a new Bash process is created.
Process Identifier (PID)
The unique number or identifier assigned to each running process is called PID (Process Identifier). The ps command displays all running processes and their PIDs.
-
The ps command displays the PIDs of all running processes. For example:
-
The pidof command also displays the PID of the running process. We can find the PID of a running process using the process name.
-
The pgrep command also displays the PID of the running process. We can grep the PID by process name using the pgrep command.
Terminating a Process in Bash
There are many options in Bash to terminate a running process. Here are some of the options you can use:
Use the Ctrl+C signal
We can interrupt or terminate the running process and send SIGINT using Ctrl+C keys. Ctrl+C sends an interrupt signal and the running program is killed or the running program is interrupted.
The above image shows the sleep 100 command being interrupted using Ctrl+C. When we use Ctrl+C to terminate or interrupt a running program, the exit code is 130 and we can use echo $?
the command.
Using the kill command
We can kill
kill a running process using the kill command. To kill a running process, we need the PID of the process.
Following is kill
the syntax of the command:
kill <pid>
The kill command uses the PID of any process to kill it. We can find the PID of any process using ps, pidof or pgrep commands.
The above image shows a process with PID 14857 being killed by the kill command.
We can also kill any process using the process name instead of its PID. pkill or killall command can kill any process with a name.
Following is the syntax of pkill and killall commands:
pkill <name>
killall<name>
All the above kill commands will send SIGKILL to the running process. If any command hangs the running process, we need to specify the signal number or signal.
The above figure shows a kill -9
process killed using the command. We can use the kill -SIGKILL <PID>
or kill -9 <PID
command to send a termination signal number or termination signal, where 9 is the SIGKILL number.
We can also use SIGKILL or 9 with the -h pkill
or -h killall
command.
The above image shows the use of pkill -9
the command to kill a sleeping process.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Move Multiple Files in Linux Bash
Publish Date:2025/04/06 Views:97 Category:OPERATING SYSTEM
-
In this article, we will explain how to move multiple files to the same directory in Linux. We will explain different methods such as typing multiple file names, using wildcard characters ( * ) to represent similar file names and/or identic
How to Compare Strings in Bash
Publish Date:2025/04/06 Views:190 Category:OPERATING SYSTEM
-
We can compare strings using various comparison operators and check if a string contains a substring using regular expressions. String comparison in Bash String comparison means checking whether the given strings are identical or not. Two o
How to Add Comments in Bash Scripts
Publish Date:2025/04/06 Views:123 Category:OPERATING SYSTEM
-
Comments are lines that are ignored by the interpreter and are only used to describe what is happening in the code or to provide insight into a particular block or line of code. Comments make it easier for the reader to understand the code.
tr Command in Linux Bash
Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM
-
In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma
Differences between Bash Profile and Bashrc
Publish Date:2025/04/05 Views:188 Category:OPERATING SYSTEM
-
This article explains the difference between ~./bash_profile and files in Bash. ~/.bashrc What are startup files in Bash? Startup files are files that are executed after the shell is started. The startup files depend on the type of shell th
Differences between Sh and Bash
Publish Date:2025/04/05 Views:145 Category:OPERATING SYSTEM
-
This article explains what a shell is, find out which shell you are currently using, check a list of all available shells, and the difference between sh and . bash What is a Shell A shell is a computer program that accepts commands. It also
How to Append Text to a File Using Bash
Publish Date:2025/04/05 Views:83 Category:OPERATING SYSTEM
-
We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th
How to Concatenate Strings in Bash
Publish Date:2025/04/05 Views:65 Category:OPERATING SYSTEM
-
String concatenation is one of the most widely used operations in programming. It refers to connecting two or more strings by putting one string at the end of another string. To concatenate strings in Bash, we can write string variables one
Configure Git Bash with Visual Studio Code
Publish Date:2025/03/31 Views:138 Category:Git
-
This article outlines the steps to configure Git Bash with Visual Studio Code on Windows. By default, VSCode uses PowerShell as the integrated terminal. We can configure VSCode to use Git Bash as the integrated terminal. Make sure you have