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
Running Regular Expressions in Case Statements in Bash
Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM
-
This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters
Solve R command not found on Bash (or Cygwin)
Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM
-
Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke
Error handling in Bash
Publish Date:2025/03/21 Views:94 Category:OPERATING SYSTEM
-
This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi
Getting the absolute path in Bash
Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM
-
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute
Difference between Bash Nohup and &
Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
Open Emacs in Bash
Publish Date:2025/03/21 Views:142 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your
Reload .bash_profile from the command line
Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM
-
In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also