JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Terminating a Process in Bash

Author:JIYIK Last Updated:2025/03/22 Views:

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.

  1. The ps command displays the PIDs of all running processes. For example:

    ps command output

  2. The pidof command also displays the PID of the running process. We can find the PID of a running process using the process name.

    pidof Command Output

    The above figure shows that a sleep command is executed in the background, and the pidof command is used to find the PID of the process.
  3. The pgrep command also displays the PID of the running process. We can grep the PID by process name using the pgrep command.

    pgrep Command Output

    The above figure shows that a sleep command is executed in the background, and the pgrep command is used to find the PID of the process.

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.

bash uses ctrl+c to terminate the process

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 killkill a running process using the kill command. To kill a running process, we need the PID of the process.

Following is killthe 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.

bash uses kill to kill the process

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.

bash kill process uses kill with signal no.

The above figure shows a kill -9process killed using the command. We can use the kill -SIGKILL <PID>or kill -9 <PIDcommand 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 pkillor -h killallcommand.

bash uses pkill with a signal number to kill the process.

The above image shows the use of pkill -9the 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.

Article URL:

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial