Killing process by name in linux

How to Kill a Process by Name in Linux: A Comprehensive Guide

Linux, much like any other operating system, relies on processes for its operations. Occasionally, these processes may become unresponsive or consume excessive system resources, necessitating their termination. The following article presents a detailed guide on how to kill a process by its name in Linux, a crucial skill for Linux users and administrators alike.

What is a Process?

Before we dive in, it’s crucial to understand what a process is. A process, in the simplest terms, is an instance of a program in execution. Each process has a unique identifier called a Process ID (PID), which the operating system uses to manage processes.

Why and When to Kill a Process?

Processes may sometimes run indefinitely, utilize excessive resources, or become unresponsive – these are the primary reasons to kill a process. However, it’s crucial to note that killing a process should be a last resort when other troubleshooting steps, such as sending a termination signal or attempting to restart the process, fail.

Finding the Process

The first step in killing a process in Linux is identifying the process. The most commonly used command for this is ‘ps’. However, given that the output of ‘ps’ can be extensive, we often use ‘grep’ to filter the results.

For instance, to find all processes related to ‘firefox’, you would use:

Here, ‘ps aux’ lists all the currently running processes, and ‘grep firefox’ filters out processes that include ‘firefox’ in their details.

Killing the Process by Name

Once you have identified the process you wish to kill, you can use the ‘kill’ command followed by the PID to terminate it. But what if you want to kill the process by name directly, without having to find the PID first? The ‘pkill’ command comes to the rescue.

The ‘pkill‘ command allows you to kill a process directly by name. For example, to kill all ‘firefox’ processes, you would use:

It’s important to note that ‘pkill’ will terminate all instances of a given process. Therefore, if you have multiple ‘firefox’ windows open, the command above will close all of them.

More Control with Signals

While ‘pkill’ is an effective and quick way to terminate processes, Linux provides you with a range of signals for more granular control.

Two important signals are SIGTERM (15) and SIGKILL (9). The SIGTERM signal is a gentle request to terminate, allowing the process to clean up before exiting. However, if a process ignores the SIGTERM signal, you can use SIGKILL, which forces the process to terminate immediately.

To send these signals with ‘pkill’, you use the ‘-signal’ option, replacing ‘signal’ with the desired signal number or name. For instance, to send a SIGTERM signal to all ‘firefox’ processes, you would use:

Читайте также:  Alma linux настройка сети

And if that fails, to send a SIGKILL signal:

Conclusion

Understanding how to manage processes is an essential part of using or administering a Linux system. With the tools and techniques explained in this article, you can efficiently kill processes by their names, enhancing your skills and ability to manage your Linux system.

Remember to use these commands responsibly, as improper use can cause data loss or system instability. Always try softer methods like SIGTERM before resorting to harsher ones like SIGKILL. Happy troubleshooting!

Источник

Killing Processes by Given Partial Names in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Process management is a fundamental aspect of Linux Administration. In this article, we’ll have a look at how to kill processes using process names.

2. Setting up a Process to Kill

Let’s create a scenario where the sleep command is executed with dummy_process as its alias name:

[[email protected] ~]$ bash -c 'exec -a dummy_process sleep 40000' &

We verify that our process is running using the ps command:

[[email protected] ~]$ ps aux |grep dummy_process user 10009 0.0 0.0 7284 728 pts/0 S 10:34 0:00 dummy_process 40000 user 10232 0.0 0.0 12112 1052 pts/0 R+ 10:44 0:00 grep --color=auto dummy_process

3. Killing a Process

3.1. Kill a Process With the Help of /proc//stat

Consequently, we kill the process using either a symbolic or a numeric signal name. In our case, we omit the signal name because the signal name SIGTERM is the default signal sent to the process:

We see that the dummy_process wasn’t killed:

[[email protected] ~]$ ps aux |grep dummy_process user 10009 0.0 0.0 7284 728 pts/0 S 10:34 0:00 dummy_process 40000 user 10582 0.0 0.0 12112 984 pts/0 S+ 11:00 0:00 grep --color=auto dummy_process

The pkill command retrieves the name of a process by reading /proc//stat:

[[email protected] ~]$ cat /proc/10009/stat 10009 (sleep) S 3292 10009 3292 34816 10721 1077936128 270 0 0 0 0 0 0 0 20 0 1 0 2785965 7458816 182 18446744073709551615 94380410159104 94380410187168 140735499122208 0 0 0 0 0 0 1 0 0 17 0 0 0 0 0 0 94380412287824 94380412289152 94380438171648 140735499125715 140735499125735 140735499125735 140735499128809 0

Furthermore, the second word in parentheses is the process name that should be specified when running pkill:

[[email protected] ~]$ pkill sleep [1]+ Terminated bash -c 'exec -a dummy_process sleep 40000'

3.2. Kill a Process With the Help of pgrep

We can kill the dummy_process using its alias name. In fact, we do this by specifying the -f option, which allows us to match the given process name with the full process name.

Assuming that we’ve recreated the dummy_process:

[[email protected] ~]$ pkill -f dummy_process [2]- Terminated bash -c 'exec -a dummy_process sleep 40000'

However, the pkill command is easy to mishandle as it will kill any process that matches the given process name. Let’s create two processes with the names dummy_process and important_dummy_process.

Let’s assume that important_dummy_process is a critical process that we don’t want to be terminated:

[[email protected] ~]$ ps aux |grep dummy user 12764 0.0 0.0 7284 788 pts/0 S 12:29 0:00 dummy_process 40000 user 12774 0.0 0.0 7284 824 pts/0 S 12:30 0:00 important_dummy_process 40000 user 12793 0.0 0.0 12112 1092 pts/0 S+ 12:30 0:00 grep --color=auto dummy [[email protected] ~]$ pkill -f dummy_process [2] Terminated bash -c 'exec -a dummy_process sleep 40000' [3]- Terminated bash -c 'exec -a important_dummy_process sleep 40000'

We see that the given name dummy_process matched both processes and killed both processes as well. This can be very dangerous. We can use the pgrep command to sift through the various matched processes and kill a specific process using its unique id number:

[email protected] ~]$ pgrep -fa dummy_process 13005 important_dummy_process 40000 13012 dummy_process 40000 [[email protected] ~]$ kill 13012 [3]- Terminated bash -c 'exec -a dummy_process sleep 40000'

Furthermore, we can use the pidof command to identify the unique id number of the process to be killed:

[[email protected] ~]$ pidof dummy_process 13788 [[email protected] ~]$ pidof dummy_process | xargs kill [3]- Terminated bash -c 'exec -a dummy_process sleep 40000'

In addition, we can specify the no-run-if-empty option. This will make sure that xargs doesn’t execute if no arguments are provided:

pidof dummy_process | xargs -r kill [3]- Terminated bash -c 'exec -a dummy_process sleep 40000'

3.3. Kill Multiple Processes Using killall

Let’s create multiple dd processes that copy nothing to nowhere and proceed by looking at what processes would be killed if we ran pkill:

[[email protected] ~]$ dd if=/dev/zero of=/dev/null & [1] 3593 [[email protected]ost ~]$ dd if=/dev/zero of=/dev/null & [2] 3601 [[email protected] ~]$ dd if=/dev/zero of=/dev/null & [3] 3608 [[email protected] ~]$ dd if=/dev/zero of=/dev/null & [4] 3615 [[email protected] ~]$ pgrep -fa dd 2 kthreadd 184 ipv6_addrconf 1067 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only 1770 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only 2018 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3 2404 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only 2506 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3 2749 /usr/libexec/evolution-addressbook-factory 2773 /usr/libexec/evolution-addressbook-factory-subprocess --factory all --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.AddressBookx2749x2 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/AddressBook/2749/2 3593 dd if=/dev/zero of=/dev/null 3601 dd if=/dev/zero of=/dev/null 3608 dd if=/dev/zero of=/dev/null 3615 dd if=/dev/zero of=/dev/null

On the other hand, the killall command provides a safer option for killing multiple processes by their name:

[[email protected] ~]$ dd if=/dev/zero of=/dev/null & [1] 3324 [[email protected] ~]$ dd if=/dev/zero of=/dev/null & [2] 3331 [[email protected] ~]$ dd if=/dev/zero of=/dev/null & [3] 3338 [[email protected] ~]$ dd if=/dev/zero of=/dev/null & [4] 3345 [[email protected] ~]$ killall dd [1] Terminated dd if=/dev/zero of=/dev/null [2] Terminated dd if=/dev/zero of=/dev/null [3]- Terminated dd if=/dev/zero of=/dev/null [4]+ Terminated dd if=/dev/zero of=/dev/null

4. Conclusion

In summary, we learned how to use the pkill command and when not to use it. Additionally, we made use of the kill command to terminate processes using a unique process id.

Читайте также:  Узнать свой видеодрайвер linux

Moreover, we looked at how to kill multiple processes in a precautionary manner by using killall.

Источник

How to kill all process with given name?

Will kill all the processes that the pattern PATTERN matches. With the -f option, the whole command line (i.e. including arguments) will be taken into account. Without the -f option, only the command name will be taken into account.

See also man pkill on your system.

The problem is that ps -A | grep | xargs -n1 returns output like this

19440 ? 00:00:11 21630 ? 00:00:00 22694 ? 00:00:00

You can use awk to a get first a column of ps output.

ps -A | grep | awk '' | xargs -n1 

And adding kill -9 $1 you have a command which kills all PIDs

ps -A | grep | awk '' | xargs kill -9 $1 

this is perfect I test it on bash script it’s kills the processer immediatly with no errors + even if the process is’nt started it shows no errors which is what I want , here example of ffmpeg processer killer , nano /usr/bin/ffmpegk . . . . ps -A | grep ffmpeg | awk ‘‘ | xargs kill -9 $1 . . . . chmod a+rx /usr/bin/ffmpegk

killall —regexp «appl.*me» Though there might be different killall implementations. See man killall .

killall not enough sometimes I need to send it three time to kill the process , and even fail to kill it , the only fast working solution fo me is kill -9 pid I think @ŁukaszD.Tulikowski is the best working solution specially for bash scripts .

pkill sends SIGTERM in default, and in my case pkill -f did’nt kill my processes. I recommend below command for such cases, which worked perfectly for me!

kill -9 $(pgrep -f somepattern) 

I also recommend to see which processes match before running kill command

Читайте также:  Утилита tar astra linux

I tried pkill -SIGKILL , it didn’t work. Can you give me a complete example of usage pkill with -9?

And I have used -w flag for ps to capture wide output in case not to miss any process name.

ps auxww | grep -E "[a]pplication_name" | awk '' 

Next, if there is valid output the results piped into xargs kill -9 command, to kill all correspondings PIDs.

ps auxww | grep -E "[a]pplication_name" | awk '' | ifne xargs kill -9 

Why the grep instead of using awk to do the test more correctly? grep will match names that include the target as substring, for example.

Two reasons: no need for an extra process, since we’re already running awk , and the grep will match more than we want — e.g. if I search for emacs , I also get emacsclient processes. And with a search pattern that matches my username, I get many other processes. Consider something like ps auxww | awk ‘gensub(«.*/»,»»,1,$11) == «‘»$prog_name»‘» ‘ for exact matching. The whole approach is flawed on platforms (such as Linux) that let applications write to the process name area, anyway. You’re better off installing the procps package, which provides pgrep and pkill .

@TobySpeight Thanks for the detailed explaination. But it only finds exact matching process names like you mention. Instead can we apply for sub-matching words like prog_n* ?

Источник

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