Linux: how to kill a process using terminal commands

Imagine: you launched a program, work with it, and after a while it stops responding and hangs. It is impossible to restart the application due to the fact that its instance still hangs in the computer's memory. What to do in this case? First of all, you need to close the program and delete the process from memory.

The easiest and most universal way to kill a process on Linux is to use terminal commands. This option will work on any Linux distribution.

Linux Processes

In the Linux operating system, a process is usually called a running program or application. Also, a process can be called any task that is currently running Linux. Any running process can start a child.

All processes running on Linux have the following attributes:

  1. PID is a unique identifier for each process.
  2. PPID is the identifier of the parent process.
  3. The identifier of the user who started the process.
  4. A priority.
  5. The state of the process.

Each running process is in one of the states:

  • "Performed". The letter R is used to indicate this condition.
  • A process that has launched a system operation, such as data input or output, and is waiting for it to complete, is called "Wait." It is designated by the letter S.
  • A process that is stopped by a user or Linux OS is in the Stopped state. This condition is indicated by T.
  • Zombies are processes that are no longer running but consume operating system resources. Indicated by the letter Z.
  • The continuous process cannot be completed until the direct memory access operation is completed. The process is indicated by D.

Process search

Before you kill a process on Linux, you must first find it. Most often, Linux uses two top and ps commands for this purpose.

Using top command

The top command displays a complete list of current processes. In addition, the list contains a lot of useful information about each running process, for example:

  • PID - a unique process number;
  • USER - login of the user who launched this process;
  • PR is the priority of the process at a given time;
  • NI - priority assigned to the process by the NICE team;
  • S is the state of this process;
  • % CPU- how much processor time does this process take (in percent);
  • % MEM - the amount of RAM that this process occupies (in percent);
  • TIME + - how much time has passed since the start of the process;
  • COMMAND is the program that started the process.
Using top command

The information provided by the top command is displayed sorted by the level of processor time used (column% CPU). User can change the sort order:

  • sort the processes by the amount of RAM used (column% MEM), press the key combination Shift + M;
  • Sort by process number (PID column) using the combination Shift + N;
  • sort by work time (TIME + column) - Shift + T.
  • return to the original sorting by the CPU load level - press the Shift + P combination.

Using ps command

Using the top command, you can get a lot of necessary information, but this is not the most convenient way to get the necessary information about the process you need. In order not to search for the desired process, you can use the ps command and filter the list using the greep command. For example, if you need to find all the processes associated with a running Opera browser, you can enter ps axu | grep Mozilla.

The ps command has the following parameters:

  • a - show processes for all users;
  • u - display the user who owns the process;
  • x - show processes that are not tied to the terminal. This option is important when searching for information about a graphical application.
Using ps command in linux

Using a bunch of ps and grep commands, you can get all the information needed to destroy a hung application.

Other ways to search for a process

In order to find the PID of a process by its name, there is a pidof command. The format of its use is as follows: pidof + program name. For example, to find all the PIDs related to the Opera program, you would enter: pidof Opera.

In some cases, it may happen that the program completed its execution, but did not release the network port. To get the information, in this case you need to enter the fuser command: fuser -vn tcp 80. Where 80 is the number of the hanging port. After that, you can kill the Linux process on the port.

Ending a process with the kill command

Once you know the PID of the process and its name, you can proceed to the operation of terminating an unnecessary application. Two commands are used for this: kill - kill the process by id in Linux and killall - end the program by name.

Command structure kill: kill SIGNAL PID. Where SIGNAL is the signal to be sent, and PID is the process identifier.

Kill command

The SIGNAL termination parameter is 9 (SIGKILL). Thus, in order to kill a process by pid on Linux, you need to enter the command in the console: kill -9 9578. Where 9578 is a unique process number.

The process termination command is not the only one that can be sent using the kill system application. To get a list of all the commands that can be sent to a command using kill, enter the kill -l command.

Ending a process with the kill command

It is also useful to know other values ​​that the SIGNAL parameter can take. For example, to reload a hung program, you can use the HUP parameter. The SIGTERM signal does not interrupt the operation of the program instantly, but gives time for the correct completion of the work, that is, the application saves data, terminates its execution, and frees up all the resources used.

How on Linux to kill a process by name

If you know the name of the program you want to complete, you can use the killall command. For example, to kill all processes related to the Opera application, you need to enter the command: killall -9 Opera.

Ending a process with the killall command

True, this command does not always complete the processes related to the program being destroyed. If, after executing the above command, you enter the combination ps aux | grep Opera and you will see that some processes are still in progress, it is best to terminate them using the kill system application.

In some cases, in Linux, killing a process started by another user may require root privileges. Then, before the process termination command, you need to enter the sudo command.

That's all the information a user needs to control processes. True, this topic is not simple for beginners, but you need to master it, since all actions of the operating system are carried out using processes. To learn how to manage them, you need to practice a little.

Source: https://habr.com/ru/post/C38369/


All Articles