Linux: how to kill a process using terminal commands

Table of contents:

Linux: how to kill a process using terminal commands
Linux: how to kill a process using terminal commands
Anonim

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

The easiest and most versatile way to kill a process in 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 a running program or application. Also, a process can be called any task that is currently running under Linux. Any running process can start a child process.

All processes running on Linux have the following attributes:

  1. PID - unique identifier for each process.
  2. PPID - parent process ID.
  3. ID of the user who started the process.
  4. Priority.
  5. Process status.

Each running process is in one of the following states:

  • "In progress". The letter R is used to represent this state.
  • A process that has started a system operation, such as input or output, and is waiting for it to complete is called "Waiting". Designated with the letter S.
  • A process that is stopped by the user or Linux OS is in the "Stopped" state. This state is indicated by the symbol T.
  • "Zombies" are processes that are no longer running, but consume operating system resources. Designated with the letter Z.
  • A persistent process cannot be terminated until the DMA operation has completed. The process is denoted by the symbol D.

Search process

Before you can kill a process in Linux, you must first find it. The most commonly used Linux commands for this purpose are top and ps.

Using the 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 - unique process number;
  • USER - login of the user who launched this process;
  • PR - process priority at a given time;
  • NI - priority assigned to the process by the NICE command;
  • S- state of this process;
  • %CPU- how much CPU time this process takes (in percent);
  • %MEM - the amount of RAM thatoccupies this process (in percent);
  • TIME+ - how much time has passed since the process started;
  • COMMAND - the program that started the process.
Using the top command
Using the top command

The information provided by the top command is sorted by the amount of CPU time used (%CPU column). The user can change the sort order:

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

Using the ps command

The top command can get you a lot of information, but it's not the most convenient way to get the information you need about the process you need. To avoid looking for the desired process, you can use the ps command and filter the list using the greep command. For example, if you want to find all the processes associated with the running Opera browser, you can type ps axu | grep Mozilla.

The ps command has the following options:

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

With the help of a bunch of ps and grep commands, you can get all the information you need to kill a hung application.

Other ways to search for a process

To find the PID of a process by its name, there is the pidof command. The format for using it is: pidof+program name. For example, to find all the PIDs related to the Opera program, you would type: pidof Opera.

In some cases, it happens that the program has completed its execution, but has not released the network port. To get information, in this case, you need to enter the fuser command: fuser -vn tcp 80. Where 80 is the number of the hung port. After that, you can kill the Linux process by port.

Ending a process with the kill command

After 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 execution by name.

Kill command structure: kill SIGNAL PID. Where SIGNAL is the signal to be sent and PID is the process ID.

kill command
kill command

The SIGNAL parameter for process termination is 9 (SIGKILL). Thus, in order to kill a process by pid in 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. Get a list of all commands that can be sentcommand with kill, you can enter the command kill -l.

Terminating a process with the kill command
Terminating a process with the kill command

It's also useful to know the other values that the SIGNAL parameter can take. For example, you can use the HUP option to reload a frozen program. The SIGTERM signal does not interrupt the program immediately, but gives time for graceful shutdown, i.e. the application saves data, terminates its execution, and releases all occupied resources.

How to kill a process by name in Linux

If you know the name of the program to be terminated, 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.

Terminating a process with the killall command
Terminating a process with the killall command

True, this command does not always end the processes related to the program being destroyed. If after running the above command you type ps aux | grep Opera and you will see that some processes are still running, it is best to kill them with the kill system application.

In some cases, in order to kill a process running by another user on Linux, root privileges may be required. Then, before the process termination command, you need to enter the sudo command.

That's all the information a user needs to manage processes. True, this topic is not easy 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 a littlepractice.

Popular topic

Editor's choice

  • The Psijic Order from The Elder Scrolls - who are they?
    The Psijic Order from The Elder Scrolls - who are they?

    The Psijics are the name of a mysterious and ancient Order from the Elder Scrolls game universe. Its members practice magic and are notorious for being at odds with the Thalmor. Even more interesting information about the Psijic Order can be found in today's guide

  • Three easy ways to turn off notifications in Viber
    Three easy ways to turn off notifications in Viber

    "Viber" today is one of the most popular and widespread instant messengers. Sometimes a situation arises that the sound of constantly incoming messages bothers or distracts a person from important matters. How to turn off notifications in Viber? The article presents three of the simplest and easiest ways to do this, and they will help you set the silent mode on both Android and iOS

  • How to advertise on Instagram yourself?
    How to advertise on Instagram yourself?

    One of the most effective methods of website promotion and business development at the moment is advertising on social networks. Social networks, where people actively communicate, are more willing to pay attention to useful posts, join groups, like posts and make reposts, are an almost inexhaustible source of traffic. A properly built advertising campaign will allow you to get new customers, keep the attention of existing ones and again win over those who were dissatisfied with the service

  • Google - what is it?
    Google - what is it?

    The article talks about the multidisciplinary corporation Google. Its services and the company's work as a device manufacturer are considered

  • How to remove a user from important friends on VKontakte: all ways
    How to remove a user from important friends on VKontakte: all ways

    Important friends of "VKontakte" are determined automatically. When friends, family, and a loved one appear first on the list, this is convenient. But relationships can change for various reasons, and the need to constantly see a person’s page may disappear. In this article, we will tell you how to remove from important VKontakte friends those users whom you do not want to see at the top of the list