Generally, Linux is known as a robust operating system. But, there are some instances where the applications get stuck and become unresponsive. When such incidents are encountered, it eats up the system’s resources badly. Furthermore, it can even result in bringing the entire system down! A nightmare, isn’t it? Well, such unfortunate incidents can occur at any time.

In such cases, it becomes necessary to terminate the ongoing process. You have the option of restarting your system or killing the process. It usually takes a lot of time to restart and may cost you inconvenience, therefore, it is recommended that you use the kill command in Linux to stop the process. Now, what is this Process that we are talking about? Let’s get your head clear!

Process

A process is nothing but the mechanism of a specific program that is executing currently. In the beginning, a unique identification number is allocated to each process. Often it is called a PID. This PID is reverted to the pool of PIDs whenever a process dies. This way some other processes can use later.

So, in several situations when these processes have to be terminated, we use the kill command. This command sends a signal to the process and hence, kills it. Now, what is a signal?

Signal

A signal is simply a response to a particular situation. A total of 64 signals exist. Every signal has different outcomes. When we talk about the first 31 signals, they are known as standard signals. And the last 33 signals are called real-time signals. 

Every process inhibits a particular behaviour. There are five types of such behaviours as mentioned below:

  • Ign
  • Core
  • Term
  • Cont
  • Stop

These behaviours give instructions to the process in order to behave in a certain way when they receive a signal. The kill command uses the TERM signal most frequently. Additionally, if a specific action is designated to a signal, we call it ‘catching’, whereas, if there occurs no action, then the signal will simply be discarded.

Take a look at the most frequently used signals:

  • SIGINT: SIGINT or Signal Interrupt, simply interrupts the given processes and terminates that process.
  • SIGKILL: This signal kills a process.
  • SIGHUP: SIGHUP or Signal hang up, results in hanging up or reloading a particular process.
  • SIGSTOP: Simply stops a process.
  • SIGTERM: Terminates a process.

Now let us move towards understanding the kill commands and using them! The kill command specifically terminates a particular process and if no signal is allocated, then the TERM signal will automatically initiate. 

Read on!

How to view the running Linux processes?

If you want a list of all the processes which are running currently, use the top command. Simply type:

top

Not only does it show the running processes, but also showcases the process IDs. In order to kill a process in this interface, just press k and then process ID. Press q to exit. The next step is to find which processes to kill. Although there are various ways to search for different processes, mostly they are located by using a process ID or process name!

How to use the ps command for locating a process?

This command delivers the same results as those delivered by the top command. This command lists the processes according to the tags that you provide. Take a look:

ps (options)

Now, look at the most general options used with this command.

  • -a: It not only views the processes of the current user but all the users.
  • -u: Details about every process.
  • -x: All the processes are under the control of daemons.

Therefore, when you type,

ps -aux

You get this on your screen! Moving on, how do you find PID?

How do you find PID?

A Linux command known as pgrep not to be confused with grep command is used to find a process in Linux. You mention certain selection criteria, precisely known as the pattern. The pattern is an expression that is incorporated into the syntax as shown below:

pgrep (options) (pattern)

pgrep -u root 

This will show every process owned by the root. 

Options:

  • -n: Returns the latest process. 
  • -l: Shows PIDs and names of the processes.
  • -o: Returns the oldest process.
  • -x: Shows processes that match the mentioned pattern.
  • -u: Shows processes of a particular user.

Whereas, the pidof command finds the process ID after you mention the process name.

pidof (options) (program)

The list of options is stated below:

  • -o: It omits specific PIDs 
  • -c: This returns only PIDs
  • -x: It helps to return PIDs of shells 
  • -s: Returns a single PID

Next up is how to use various kill command options.

Commands

Kill Command

Use the following syntax to implement the kill command:

kill (options) (PID)

Now, if you already know the process ID of a process, you can easily invoke the command like this:

[root@host ~]# kill 215

This command simply invokes the TERM signal. If this command is not able to stop the process, use SIGKILL to kill it. It does not matter if the processes are not closed yet, it doesn’t wait for them. Take a look:

[root@host ~]# kill -SIGKILL 215

As you know, there are various methods to kill a process, here is a list of the commands used to achieve the task.

killall command

If you want to kill multiple processes by name with a single command, then use this command. It sends a SIGTERM signal as mentioned above in the article. Here is the syntax:

killall (process)

Take a look at the options involved in killall command:

  • -i: This option asks for confirmation while it kills the process
  • -e: Helps to find the same match for a process name
  • -v: Reports if a process is successfully killed
  • -u: Kills the processes belonging to specific users

You can also use this command based on the duration of the process. For more clarity, read on.

killall -o 30m

This command kills the process that exists for more than 30 minutes. Similarly, 

killall -y 30m

This command kills the process that exists for less than 30 minutes.

kill -9 command

If any process or service becomes unresponsive you can shut down the system using this command.

kill -9 (process ID)

Or

kill -SIGKILL (process ID)

pkill command

pkill command kills a process according to the mentioned process names. 

pkill (options) (pattern)

The options are as follows:

  • -o: Kills the oldest process
  • -signal: Helps to send a particular signal other than SIGTERM
  • -n: Kills the latest process
  • -x: Matches the pattern and shows the process with the exact match
  • -u: Kills those processes allocated to a specific user

xkill command

It forces a server to close connections to its clients. Additionally, if various unwanted processes are running, this command helps to abort all of them.

xkill (resources)

Major Takeaways

We have reached the end of this article. So, in a nutshell, if due to any circumstances, the process can not be closed in Linux, we use the kill command. Start by finding the process with the help of the above-mentioned commands like kill, killall, pkill, xkill, etc.  

If you're looking to host a website or run complex applications, buy linux vps can provide you with the power, flexibility, and security you need to meet your goals.

Moreover, you can also use several signals while killing any process. Do not forget that to kill a process, you need permission. Use the sudo command for the same. We hope that this article helped you to solve your queries regarding the topic. Keep practising, keep shining!

People also read: