How to kill a Linux process. Process management in Linux Linux start a process with a specific pid


We will show you how to kill a process in Linux. One of the main advantages of Linux is the ability to terminate a process without having to reboot the server. In this article, we will show you how to kill a process in Linux using the kill, pkill and killall command.

1. What is PID

Before we begin, we need to know what a Process ID (PID) is.

PID represents the digital identification of a process in . Each process has a unique PID. In fact, for example, the first process that starts on a Linux-based system is a process and its PID is set to 1. This process is the parent of all other processes. The init process cannot be killed using kill commands, and this ensures that it will not be killed accidentally.

Now, in order to find the PID of each running process on the server, we can run the following command:

This gives us a list of all running processes and their corresponding PIDs.

If we want to find the PID of a specific process, we can use the pidof command followed by the process name. For example, to find out the PID of our MySQL process, you can run the following command:

Pidof mysql

For even more detailed information, we can use the command ps aux together with grep:

PS aux | grep mysql

Now that we know what a PID is and how to find the PID of a specific process, we can move on to the next section and learn how to kill it.

2. Kill a process using the kill command in Linux

There are a few important rules that we need to know before we start using the kill command.

  • You can only kill native processes that belong to your user ID
  • You cannot kill other users' processes
  • You can't kill system processes (unless you are superuser)
  • The root user can kill any other user's process and any system process

When we kill a process with the kill command, we are actually sending the specific PID signal we want to kill. The following signals are used by the kill command:

1 = Hung up 9 = Kill 15 = Terminate

The hung up signal is rarely used. Most often we use the kill signal, and if it doesn't work, then we can use the Terminate signal.

So once we find the PID of the process we want to kill, use one of the methods we described earlier, we can use the command kill -9 PID to kill the process from that specific PID.

For example, if the PID is 6738, then we can use the following command:

Kill -9 6738

3. Kill a process using pkill command in Linux

If you want to use the process name instead of its PID to kill it, then you can use the pkill command. For example, if the process we want to kill is called , then we can use the following command to kill it:

Pkill mysql

4. Kill a process using the killall command in Linux

The two previous commands are used to kill only one specific process. But, if we want to kill a process along with all its child processes we can use the command killall:

Killall mysql

In this example, we will kill the MySQL process and all its child processes.

They are the most common examples of process killing in Linux.

Be that as it may, some applications in Linux sometimes they freeze. At the same time, there are situations when the application does not respond at all, or works so slowly that it is not possible to terminate its work correctly. To quickly get out of the resulting situation, you can “kill” this process. To do this, use the commands kill And killall. Now we will figure out how to use these commands, find the PID of the process and send the SIGKILL signal.

To avoid confusion, let’s agree that by process we mean a program launched into the system. For example, if you have several Mozilla Firefox browser windows running, this means that three processes are running.

Determine the PID of a process - pidof command

PID— unique process identifier in the system Linux. To correctly stop a process, you should first determine its PID. To do this, use the ps and grep commands. In its turn ps command is designed to display a list of active processes in the system and information about them. grep command runs simultaneously with ps (in a channel) and will search the results of the ps command. You can list all processes by running on the command line:

Of course, PID can also be determined using top. But in most cases, the number of processes is too large (and changes dynamically in top), so it is not so easy to quickly and correctly determine the PID. This is exactly what the grep command is used for. For example, to terminate the Google Chrome browser process, you need to run the following command:

p.s. | grep chrome

$ ps axu | grep chrome
itechf2 20474 2.7 1.5 938416 120136 tty2 Sl+ 11:07 0:00 /opt/google/chrome/chrome

In our case, 20474 is the desired PID. An easier way is to use the command pidof, you must specify the process name. For example:

$pidofchrome
20728 20706 20668 20647 20586 20574 20553 20508 20474

Terminate a process in Linux - kill and killall commands

Terminate a process in the operating system Linux, knowing its PID, you can use the command kill. It's worth knowing and understanding: the kill command is designed to send a signal to a process. By default, if we do not specify which signal to send, the SIGTERM signal (from the word termination) is sent. SIGTERM tells the process to terminate. Each signal has its own number. SIGTERM is numbered 15. A list of all the signals (and their numbers) that the kill command can send can be obtained by running kill -l . To send the SIGKILL signal (numbered 9) to process 2811, run on the command line:

At the same time, the SIGTERM signal may not stop the process (for example, when the signal is intercepted or blocked), but SIGKILL always kills the process, since it cannot be intercepted or ignored.

killall command in Linux is designed to “kill” all processes with the same name. This is convenient because we don't need to know the PID of the process. For example, we want to close all processes named chrome. Run in terminal:

The killall command, like kill, sends the SIGTERM signal by default. To send another signal you need to use the option -s . For example:

Basically we look at the PID to kill the unresponsive program and it is similar to the Windows Task Manager.

Linux GUI also offers the same feature, but CLI is an efficient way to perform the kill operation.

What is the Process ID PID?

PID stands for process identification number, which is commonly used by most operating system kernels such as Linux, Unix, macOS and Windows.

This is a unique identification number that is automatically assigned to each process when it is created in the operating system.

A process is an executable instance of a program.

Each time, the process ID will receive changes to all processes except init, since init is always the first process in the system and is the ancestor of all other processes. This PID is 1.

The default maximum PID value is 32,768.

# cat/proc/sys/kernel/pid_max

On 32-bit systems, 32768 is the maximum value, but we can set any value up to 2^22 (approximately 4 million) on 64-bit systems.

You may ask why we need so many PIDs? because we can't reuse the PID right away. Also to avoid possible mistakes.

The PID for running processes in the system can be found using the following nine methods such as pidof command, pgrep command, ps command, pstree command, ss command, netstat command, lsof command, fuser command and systemctl command.

  • pidof: pidof - find the process ID of a running program.
  • pgrep: pgre - Search or process signals based on name and other attributes.
  • ps: ps - reports a snapshot of current processes.
  • pstree: pstree - displays a process tree.
  • ss: ss is used to display socket statistics.
  • netstat: netstat displays a list of open sockets.
  • lsof: lsof - list of open files.
  • fuser: process IDs in a term list of all processes that open one or more files
  • systemctl: systemctl - Manage systemd system and service manager

In this tutorial we will look at the Apache process ID for verification.

Method-1: Using pidof command

pidof is used to find the process ID of a running program.

It prints these identifiers to standard output.

To demonstrate this, we will find out the process ID of Apache2 from a Debian 9 system.

# pidof apache2 3754 2594 2365 2364 2363 2362 2361

From the above, you may find it difficult to identify the process ID as it shows all PID (including parent and child) with the process name.

Therefore, we need to figure out the parent PID (PPID) that we are looking for.

This could be number one. In my case it is 3754 and it is shown in descending order.

Method-2: Using pgrep command

pgrep looks at the current processes and lists the process IDs that match the selection criteria for stdout.

# pgrep apache2 2361 2362 2363 2364 2365 2594 3754

This is also similar to the above output, but this one reduces the results in ascending order, which clearly indicates that the parent PID is the last one.

In my case it is 3754.

Note. If you have multiple process IDs, you may have trouble identifying the parent process ID when using the pidof & pgrep command.

Method-3: Using pstree command

pstree shows running processes as a tree.

The tree is rooted either at pid or at init if pid is omitted.

If a username is specified in the pstree command, then the entire process owned by the corresponding user is displayed.

pstree visually combines identical branches by placing them in square brackets and prefixing them with the number of repetitions.

#pstree -p | grep "apache2" |- apache2(3754)-+-apache2(2361) | |-apache2(2362) | |-apache2(2363) | |-apache2(2364) | |-apache2(2365) | `-apache2(2594)

To get only one parent process, use the following format.

#pstree -p | grep "apache2" | head -1 |- apache2(3754)-+-apache2(2361)

The pstree command is very simple because it separates the parent and child processes separately

Method-4: Using ps command

ps displays information about the selection of active processes.

It displays the process ID (pid = PID), terminal associated with the process (tname = TTY), cumulative CPU time in hh:mm:ss format (time = TIME), and executable name (ucmd = cmd).

By default, the output file is not sorted.

# ps aux | grep "apache2" www-data 2361 0.0 0.4 302652 9732 ? S 06:25 0:00 /usr/sbin/apache2 -k start www-data 2362 0.0 0.4 302652 9732 ? S 06:25 0:00 /usr/sbin/apache2 -k start www-data 2363 0.0 0.4 302652 9732 ? S 06:25 0:00 /usr/sbin/apache2 -k start www-data 2364 0.0 0.4 302652 9732 ? S 06:25 0:00 /usr/sbin/apache2 -k start www-data 2365 0.0 0.4 302652 8400 ? S 06:25 0:00 /usr/sbin/apache2 -k start www-data 2594 0.0 0.4 302652 8400 ? S 06:55 0:00 /usr/sbin/apache2 -k start root 3754 0.0 1.4 302580 29324 ? Ss Dec11 0:23 /usr/sbin/apache2 -k start root 5648 0.0 0.0 12784 940 pts/0 S+ 21:32 0:00 grep apache2

From the above output, we can easily identify the Parent Process ID (PPID) based on the process start date.

In my case the apache2 process was started by @Dec11 which is the parent and the others are the children. Apache2 PID is 3754.

Method-5: Using ss command

ss is used to display socket statistics.

It allows you to display information similar to netstat.

It can display more TCP and status information than other tools.

It can display statistics for all types of sockets such as PACKET, TCP, UDP, DCCP, RAW, Unix domain, etc.

# ss -tnlp | grep apache2 LISTEN 0 128:::80:::* users:(("apache2",pid=3319,fd=4),("apache2",pid=3318,fd=4),("apache2", pid=3317,fd=4))

Method-6: Using netstat command

netstat - Displays network connections, routing tables, interface statistics, masquerading and multicast connections.

By default, netstat displays a list of open sockets.

If you do not specify any address families, the active sockets of all configured address families will be listed.

This program is outdated. The replacement for netstat is ss.

# netstat -tnlp | grep apache2 tcp6 0 0:::80:::* LISTEN 3317/apache2

Method-7: Using lsof command

lsof - list of open files.

The lsof Linux command displays information about files open to processes running on the system.

# lsof -i -P | grep apache2 apache2 3317 root 4u IPv6 40518 0t0 TCP *:80 (LISTEN) apache2 3318 www-data 4u IPv6 40518 0t0 TCP *:80 (LISTEN) apache2 3319 www-data 4u IPv6 40518 0t0 TCP *:80 (LISTEN)

Method-8: Using fuser command

The fuser utility must write to standard output the process IDs of processes running on the local system that open one or more named files.

# fuser -v 80/tcp USER PID ACCESS COMMAND 80/tcp: root 3317 F.... apache2 www-data 3318 F.... apache2 www-data 3319 F.... apache2

Method-9: Using systemctl command

systemctl - Manage the systemd system and service manager.

This is a replacement for the old SysV system management and Most modern Linux operating systems have adopted systemd.

# systemctl status apache2 ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service. d └─apache2-systemd.conf Active: active (running) since Tue 2018-09-25 10:03:28 IST; 3s ago Process: 3294 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Main PID: 3317 (apache2) Tasks: 55 (limit: 4915) Memory: 7.9M CPU: 71ms CGroup: /system.slice/apache2.service ├─3317 /usr/sbin/apache2 -k start ├─3318 /usr/sbin/apache2 -k start └ ─3319 /usr/sbin/apache2 -k start Sep 25 10:03:28 ubuntu systemd: Starting The Apache HTTP Server... Sep 25 10:03:28 ubuntu systemd: Started The Apache HTTP Server.

Despite the fact that Linux is more stable than Windows in terms of the operation of programs and various services, anything can happen and sometimes it becomes necessary to terminate the Linux process. This may be necessary if the program crashed when you launched a system service in the background through the terminal rather than the initialization system, as well as in many other cases when it is easier to kill the Linux process by rebooting the entire system.

In this article, we will look at some of the most common ways to terminate a Linux process. We will describe in detail how the process is stopped and how to do everything correctly.

Processes in the Linux operating system are controlled using signals. Including the completion of any process. Signals are transmitted by the system, but they can also be transmitted by the user using special commands or even keyboard shortcuts in the terminal. When a process receives a signal to terminate, it must perform some preparatory steps.

It is necessary to terminate child processes, delete temporary files, sockets, and so on. But depending on the complexity of the situation, the process may not respond to all signals. Let's look at the main signals that are used to complete the process:

  • SIGINT- the most harmless termination signal, means Interrupt. It is sent to the process launched from the terminal using the Ctrl+C keyboard shortcut. The process correctly completes all its actions and returns control;
  • SIGQUIT - this is another signal that is sent, using a keyboard shortcut, to a program running in the terminal. It tells it to exit, and the program can terminate gracefully or ignore the signal. Unlike the previous one, it generates a memory dump. Keyboard shortcut Ctrl+/;
  • SIGHUP- informs the process that the connection with the control terminal is broken, sent mainly by the system when the connection to the Internet is broken;
  • SIGTERM- immediately terminates the process, but is processed by the program, so it allows it to terminate child processes and free all resources;
  • SIGKILL- also immediately terminates the process, but, unlike the previous option, it is not transferred to the process itself, but is processed by the kernel. Therefore, resources and child processes remain running.

It is important to understand that you need to give the process the opportunity to complete correctly. It is advisable that ports and sockets are freed, temporary files are closed and temporary files are deleted. Therefore, never send SIGKILL straight away. Send termination signals in the sequence as listed above.

At first Ctrl+C, if possible, then SIGTERM - although it terminates the process, it does this culturally, and only as a last resort SIGKILL. Now let’s look at how to kill a process using pid Linux in practice. If you always use SIGKILL, then this picture comes to mind:

How to kill a Linux process?

The kill utility is used to signal processes in Linux. Its syntax is very simple:

$ kill - process pid signal

The signal is one of the above signals to complete the process. By default, if this parameter is not specified, the SIGTERM signal is used, which is very correct. We also need to indicate which process needs to be terminated. To do this, a unique process identifier - PID - is used.

Let's say we are running the ping utility. We want to terminate it using kill. Then, first we find out its identifier using the ps command:

ps aux | grep ping

The first line will display the ping utility itself, and the second line will display the ps program itself. We take the desired PID and terminate the process using SIGTERM:

kill -TERM 20446

And only if after this command the process continues to hang, and you can check this by running ps. Only now you can perform SIGKILL:

kill -KILL 20446

Now let's check again:

If the process is running as superuser, then naturally you need to use sudo. It is not always convenient to kill a process by its PID, at least because you still need to find out this PID. We could make complex constructs using xargs to automatically calculate the pid from the process name and terminate it immediately, but this is not necessary. Special utilities already exist.

How to kill a process using pkill

The pkill utility is a wrapper around kill, it behaves exactly the same and has the same syntax, only it needs to pass the process name as the process identifier. The utility scans the proc directory and finds the PID of the first process with that name, then sends a SIGTERM to it. This way you can kill the process called Linux. For example, if we want to complete the same ping:

You can also manually set the signal type:

pkill -TERM ping

Instead of ps, you can use the pgrep utility to find the pid of the process, making sure our program is completed:

But if the program has created several processes for you, for example, the chromium or firefox browser creates a separate process for each of the tabs, then this utility will be of little help. Here we need the next option.

How to stop a process using killall

killall works similarly to the two previous utilities. It also takes the process name as a parameter and looks for its PID in the /proc directory. But this utility will detect all processes with that name and terminate them. For example:

As you can see, several processes are running, all that remains is to stop the Linux process using killall:

The command will terminate all running ping utilities, you can verify this by running pgrep again:

conclusions

In this article, we looked at how to kill a Linux process. This task can be very rewarding at times, but it is important to understand that it needs to be done correctly. This is not to say that passing SIGKILL instead of SIGTERM is very dangerous, but it is not worth doing. I hope this information was useful to you.







2024 gtavrl.ru.