Press Ctrl/Cmd + P to print
or save as PDF

Linux VPS Server Guide on Killing a Process

All the tasks run in a Linux operating system is called a process, and every process has a unique identifier called process ID. You can improve your VPS management skill by learning how to terminate a process with the kill command. The kill command allows you to stop any process for starting a process by accident or any process for any reason.

In this article, we will guide you on how to use the Linux kill command in your Linux VPS server.

Reveal Process ID

To kill a process, you must first get the identifier of the process called process ID, also known as pid. Run the following command to show the pid.

ps

You may list out specific processes by using the grep command to filter out the process such as the following command.

ps -ux | grep java

This command will filter out all the running java processes and display them in the output.

Display Kill Command Signals

To display a manual page entry of different kill signals, run the following command.

kill -l

This will display all the names and numbers of the kill signals. However, the kill signals that are likely to be used are only SIGKILL (9) and SIGTERM (15).

Kill Processes In Linux with Terminal

To use the terminal to kill the processes you need, firstly, access your server using SSH.

Kill Specific Process

To kill a specific process, first, get the pid, then run the following command in your terminal.

kill <pid>

Replace the “<pid>” with the process ID of the process you want to stop. No signal is specified in this killing process, hence it is a SIGTERM signal. However, there are times that the process could not be terminated. To kill such processes forcefully, use the following command format to assist you.

kill <Signal_or_Option> <pid>

You can forcefully kill the process using the “SIGKILL” signal, or the “-9” option.

Kill Multiple Processes

To kill multiple processes at once, you may use the similar command with multiple pid with a space in between them such as the following command.

kill -9 <pid1> <pid2> <pid3>

Pkill Command

The pkill command allows you to kill a command with the process name. For example, if you want to kill the chrome process, you can run the following command.

pkill chrome

You will be able to kill chrome with only a partial name match too such as the following example.

pkill chr

The above command will also kill the chrome process. However, this command comes with a great risk where it will delete all processes with the matching name, which means you may close some other processes unintendedly.

If you know the complete name of the process, you may check the list using the following command.

pidof chrome

You can filter out all the processes with the similar partial name using the following command.

pgrep -l chr

Killall Command

The killall command is somewhat similar to the kill and pkill command, where it only kills the named process, however, similar to pkill, it doesn’t need a pid. However killall command, unlike pkill, doesn’t kill all the processes with the same patter matched name, but only the named process. This makes killall much safer compared to pkill.

Aside from that, you may customize when to kill the process after a set duration of running. For example, if you want to kill a process after running for 2 hours, run the following command.

killall -y 2h <process_name>

The “2h” in the command meant 2 hours. To set any duration you need in the command, follow the following time format.

  • y – refer to years
  • M – refer to months
  • w – refer to weeks
  • d – refer to days
  • h – refer to hours
  • m – refer to minutes
  • s – refer to seconds

That is all for this guide. Hopefully, you find this article helpful in managing the processes in your VPS.