Terminating processes in linux

How to Kill a Process in Linux

Have you ever faced the situation where you launched an application, and suddenly while you are using the app, it becomes unresponsive and unexpectedly crashes? You try to start the application again, but nothing happens because the original application process never truly shuts down completely.

Well, it has happened to all of us at some point, hasn’t it? The solution is to terminate or kill the application process. But how?

Luckily, there are several utilities in Linux that allows you to kill errant processes.

In this article, we will show you how to use kill , killall , and pkill commands to terminate a process in Linux.

The main difference between these tools is that kill terminates processes based on Process ID number (PID), while the killall and pkill commands terminate running processes based on their names and other attributes.

Regular users can kill their own processes, but not those that belong to other users, while the root user can kill all processes.

System Kill Signals #

kill , killall , and pkill send a given signal to specified processes or process groups. When no signal is specified, each tool sends 15 ( TERM ).

The most commonly used signals are:

  • 1 (-HUP): to reload a process.
  • 9 (-KILL): to kill a process.
  • 15 (-TERM): to gracefully stop a process.

Signals can be specified in three different ways:

  • using a number (e.g., -1)
  • with the “SIG” prefix (e.g., -SIGHUP)
  • without the “SIG” prefix (e.g., -HUP).

Use the -l option to list all available signals:

The steps outlined below will work on all Linux distributions.

Terminating Processes Using the kill Command #

To terminate a process with the kill command first, you need to find the process PID. You can do this using different commands such as top , ps , pidof , and pgrep .

Let’s say the Firefox browser has become unresponsive, and you need to kill the browser process. To find the process ID, use the pidof command:

The command will print all Firefox processes:

Once you know the Firefox processes PIDs to terminate all of them send the TERM signal:

kill -9 2551 2514 1963 1856 1771

Terminating Processes Using the killall Command #

The killall command terminates all programs that match a specified name.

Using the same scenario as before, you can kill the Firefox process by typing:

killall accepts several options such as sending signals to processes owned by a given user, matching processes names against regular expressions, and the creation time. You can get a list of all options by typing killall (without any arguments) on your terminal.

Читайте также:  Linux повысить приоритет процесса

For example, to terminate all processes running as a user “sara”, you would run the following command:

Terminating Processes Using the pkill Command #

pkill terminates processes that match the pattern given on the command line:

The name of the process doesn’t have to be an exact match.

With pkill you can also send a signal to processes that are owned by a given user. To kill only the firefox processes owned by the user “sara”, you would type:

Conclusion #

Terminating unresponsive programs using the kill , killall and pkill commands is an easy task. You only need to know the process name or PID.

If you have any questions or feedback, feel free to leave a comment.

Источник

How to Kill a Process in Linux? Commands to Terminate

If a Linux process becomes unresponsive or is consuming too many resources, you may need to kill it.

Most processes have their own methods of shutting down. Unfortunately, processes can malfunction and not allow themselves to be shut down. If a running background process is unresponsive, it becomes necessary to use a command to kill it.

Here’s a complete guide on how to kill a Linux process using the command line.

how-to-kill-a-process-in-linux

What Processes Can You Kill in Linux?

Before killing or terminating a process, you need to consider permissions.

A root user can kill all processes. You can either add sudo before a command to run it as root, or obtain a root shell with su . Then execute the command.

Killing a process sends a termination message to the given process. There are multiple types of termination messages including:

  • SIGKILL – SIGKILL is the ultimate way of killing a process. It will always kill a process and will kill the process abruptly, generating a fatal error. SIGKILL should always work. If it does not work, the operating system has failed.
  • SIGTERM – SIGTERM attempts to kill a process, but unlike SIGKILL it may be blocked or otherwise handled. It can be considered a gentler way of attempting to terminate a process.

For most purposes, SIGKILL will be the fastest and most effective method to terminate the process.

Step 1: View Running Linux Processes

The top command is the easiest way to get a complete overview of the processes currently being run.

To view a list of all currently running processes, use the command:

The top command will reveal process IDs and users, in addition to the amount of memory and CPU power each process is using.

Top command kill process in Linux

To kill processes directly from the top interface, press k and enter the process ID.

To exit the top interface, press q.

Step 2: Locate the Process to Kill

Before you can kill a process, you need to find it. There are multiple ways you can search for a process in Linux. Processes can either be located by a process name (or a partial process name) or a process ID (also known as a “pid”).

Locate a Process with ps Command

The ps command displays similar information to top , though it will not be in the form of an interface. Instead, the ps command provides a complete listing of running processes, formatted based on the tags you add.

Читайте также:  Настройка статичного ip linux

The most common options to add to this is “-aux”:

  • -a . View processes of all users rather than just the current user.
  • -u . Provide detailed information about each of the processes
  • -x . Include processes that are controlled not by users but by daemons.

For example, the command ps -aux will return a detailed process list of all processes.

ps aux kill process in linux

Finding the PID with pgrep or pidof

The Linux command pgrep is a more complex way of finding a process. This command will return processes based on specific selection criteria, which is known as the pattern. The pattern is a regular expression, such as a* , where * would be a wildcard.

Here are the options that can be used with this command:

  • -l . List both the process names and the PIDs.
  • -n . Return the process that is newest.
  • -o . Return the process that is oldest.
  • -u . Only find processes that belong to a specific user.
  • -x . Only find processes that exactly match the given pattern.

The command pgrep -u root displays all processes owned by root. The command pgrep -u root ‘a*’ returns processes owned by root that start with the letter “a”.

The pidof command is used to find the ID of a process, provided that you know the name of the process.

A few options can be included, such as:

  • -c . Only return PIDs within a single root directory.
  • -o . Omit certain PIDs (include the processes to omit after the flag).
  • -s . Only return a single PID.
  • -x . Also returns PIDs of shells that are running scripts.

Step 3: Use Kill Command Options to Terminate a Process

There are a few different methods of killing a process in Linux, depending on whether you know the name of the process running, the pid of the process, or just how long the process has been running.

killall Command

The killall command is used to kill processes by name. By default, it will send a SIGTERM signal. The killall command can kill multiple processes with a single command.

Several options can be used with the killall command:

  • -e . Find an exact match for the process name.
  • -I . Ignore case when trying to find the process name.
  • -i . Ask for additional confirmation when killing the process.
  • -u . Only kill processes owned by a specific user.
  • -v . Report back on whether the process has been successfully killed.

In addition to killing processes based on name, the killall command can also be used to kill based on the age of the process, using the following commands:

  • -o . Use this flag with a duration to kill all processes that have been running more than that amount of time.
  • -y . Use this flag with a duration to kill all processes that have been running less than that amount of time.

The killall -o 15m command will kill all processes that are older than 15 minutes, while the killall -y 15m command will kill all processes that are less than 15 minutes.

Читайте также:  Mingw w64 on linux

pkill Command

The pkill command is similar to the pgrep command, in that it will kill a process based on the process name, in addition to other qualifying factors. By default, pkill will send the SIGTERM signal.

  • -n . Only kill the newest of the processes that are discovered.
  • -o . Only kill the oldest of the processes that are discovered.
  • -u . Only kill the processes that are owned by the selected user.
  • -x . Only kill the processes that match the pattern exactly.
  • -signal . Send a specific signal to the process, rather than SIGTERM.

kill Command

If you know a process ID, you can kill it with the command:

The kill command will kill a single process at a time with the given process ID. It will send a SIGTERM signal indicating to a process to stop. It waits for the program to run its shutdown routine.

The -signal command can be used to specify a signal that isn’t SIGTERM.

kill -9 Linux Command

kill -9 is a useful command when you need to shut down an unresponsive service. Run it similarly as a regular kill command:

The kill -9 command sends a SIGKILL signal indicating to a service to shut down immediately. An unresponsive program will ignore a kill command, but it will shut down whenever a kill -9 command is issued. Use this command with caution. It bypasses the standard shutdown routine so any unsaved data will be lost.

Your operating system is not running properly if a SIGKILL signal does not shut down a service.

top Command

The top command provides an interface through which a user can navigate through currently running processes.

To kill a specific process, insert k from the top interface and then enter in the desired process ID.

kill-top-command-linux

xkill command

The xkill command is a special type of command that closes a given server’s connection to clients.

If a server has opened a number of unwanted processes, xkill will abort these processes.

If xkill is run without specifying a resource, then an interface will open up that lets the user select a window to close.

Key Takeaways on Terminating a Linux Process

  • When a process cannot be closed any other way, it can be manually killed via command line.
  • To kill a process in Linux, you must first find the process. You can use the top , ps , pidof or pgrep commands.
  • Once you have found the process you want to kill, you can kill it with the killall , pkill , kill , xkill or top commands.
  • When killing a process, you can send a termination signal of SIGHUP, SIGKILL, or SIGTERM.
  • You need to have permission to kill a process, which you can gain through the use of the sudo command.

Note: Learn how to use the nohup command to block the SIGHUP signal and allow processes to complete even after logging out from the terminal/shell.

In this article, we covered several ways to kill processes in Linux. It is critical to learn and understand these Linux termination commands for system management and administration.

Источник

Оцените статью
Adblock
detector