How to kill a process on Linux

Configurare noua (How To)

Situatie

Everything that’s running on a Linux system – a service, script, or anything else – is considered a “process”. If you need to end a running process on Linux, the kill command is sure to do the job.

Solutie

Using the System Monitor

The Gnome system monitor is a built-in GUI utility that can be used to kill a process in Linux OS. It allows to stop a process and then resume it with the Continue option. The end option allows terminating a process safely while the kill option forcefully terminates the program.

If System Monitor is not installed in your system, you can install it as follows:

$ sudo apt-get install gnome-system-monitor

To launch System Monitor, hit the super key and search it using the search bar at the top. When the search result appears as follows, hit Enter to open it.

In the System Monitor window, you will see a list of processes running in your system. In order to kill a process, right-click it, and select Kill option. It will kill the selected process instantly.

For a kill command a Signal Name could be:

  • SIGTERM is the default and safest way to kill a process.
  • SIGHUP is a less secure way of killing a process than SIGTERM.
  • SIGKILL is the most unsafe way among the above three, to kill a process that terminates a process without saving.

In Linux, there are different signals you can use to stop, end, or pause processes. You can list all available kill signals with their names and corresponding numbers using the following command, which will list

$ kill -l

While there are multiple available signals, in most cases we only use SIGKILL (9) and SIGTERM (15).

Kill process using the keyboard shortcuts in Terminal

Linux command line includes a number of useful keyboard shortcuts. Among them, following shortcuts can be used to kill a running process.

Ctrl+C: It sends SIGINT that terminates the running process

Ctrl+Z: It sends SIGSTP that suspends an application and send it to the background. However, it does not terminate the process. To view the stopped process, you can use the jobs command. Use fg command to bring the process to the foreground.

Ctrl+\: It sends SIGQUIT that terminates the process. It also creates a core dump file which can be used to debug the process.

Kill process using the xkill utility

Xkill allows killing a running program using the mouse cursor. It is GUI based utility that is pre-installed in most of the systems. If not already installed, you can install it as follows:

$ sudo apt install xorg-xkill

To close any program, simply type this in your command line Terminal:

$ xkill

Running the above command will turn your mouse cursor in to x shape. Now place the cursor on the program that you want to close and left-click on it.

Set shortcut for Xkill

You can set a shortcut for xkill that will allow you to immediately kill an application without the need of opening the Terminal and running the command.

To create a shortcut for xkill, open the Settings utility using the right-click menu from the desktop. Then open the Keyboard Shortcuts tab and click the + icon at the very bottom.

Then in the following dialog, name the shortcut and type xkill in the Command field and click Set Shortcut button.

Then set a custom shortcut of your choice and click the Add button.

Now whenever you need to kill an application, simply press the shortcut keys and you will be able to kill any open application in your system.

Kill process using the Kill commands

There are also some command-line ways used to kill the processes in Linux which includes kill, pkill, and killall.

To find which processes are currently running in your system, you can use the ps command with –A flag:

$ ps –A

It will list all the currently running processes in your system.

Kill

Kill command can be used to kill a running process in Linux. The kill command is provided with a PID of a process to be killed.

To find the process ID of a running process, you can use ps –A command. Alternatively, you can pipe the output of ps with grep command to find the process ID of a specific process:

$ ps –A | grep <processname>

For example:

To find the process ID of a running Firefox program, you can use:

$ ps –A | grep firefox

Once you have found the PID of a specific process, you can kill it as follows:

$ kill PID

The kill command sends a SIGTERM signal to the specified PID which asks the process to terminate after performing the necessary cleanup operation.

In some scenarios, running the kill command does not terminate the process. If this is the case, you will need to type “kill -9” followed by the PID:

$ kill -9 PID

Using the -9 option with kill command sends a SIGKILL signal which asks the process to terminate immediately without any cleanup operation.

Pkill

Similar to kill command, pkill also sends a SIGTERM signal which allows terminating an unresponsive process. However, the good thing about pkill is that you do not have to provide the PID of a process in order to kill it. Instead, you can just provide the matching keyword related to the process.

$ pkill <keyword>

For example, to kill Firefox program, you can just type:

$ pkill firef

It will kill all the processes whose names match with the mentioned <keyword>.

With pkill, you also have an option to kill the process running by a specific user:

$ pkill –u <username> < keyword>

Be careful when using this option as If you do not specify the <keyword>, all the processes with the specified username will be killed.

Killall

Killall command is similar to pkill except it takes the full process name as an argument instead of any matching keyword.

In order to use killall to terminate all the processes and their child processes with a specific name, use the following syntax:

$ killall <prcoessname>

For example:

$ killall firefox

Tip solutie

Permanent

Voteaza

(9 din 15 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?