Sometimes I have a desire to kill many processes using just one line of my BASH console. All this stuff like finding out PIDs from top
or ps
commands, killing them manually makes me suffer!
Deal, it has to be a shell-script in order to automate it!
First of all we got to create an empty file to put commands into it, let us do it:
touch kill-all.sh
Once the file is created we have to make it executable, easily:
chmod +x ./kill-all.sh
Now we have a script that could be run and we got to fill it with some helpful instructions:
#!/bin/bash
# Defining a proc name
proc_name=$1
# Clearing stdout
clear
# Performing killing
ps -aux | grep "$proc_name" | grep -v grep | cut -c 10-15 | xargs sudo kill -9
It's done! Now if you are going to kill many processes with same name you just need to run this quite simple instruction:
./kill-all.sh slack
It will lead you to close all the processes with name slack
Thanks for reading this article! You may follow me in order to be updated with most new articles from my side.
As usual if you have anything just let me know about it in the comments!
Top comments (10)
How does what you're doing, above, differentiate from just using
pkill
and related tools?That's a good question and the answer would be
nothing
, sopkill
does that out from the box and you could easily use it instead!Honestly I just tried first solution off the top of my head, so that works fine for me, but thanks for the good advice anyway Β―_(γ)_/Β―
Ok. Cool. I used to do similar (back in my Solaris days before Sun finally got around to adding the ps-tools to their OS). Looked similar to that, but I wanted to make sure I wasn't missing something. =)
No, you mayn't have to worry, they are the same things!
So, a few things.
killall is already a command that is widely installed and very commonly used. It serves exactly this purpose. Two commands, even, if you consider the older SystemV and Solaris program called killall that just kills all processes and takes no arguments.
This approach is extremely unsafe because you havenβt bounded your grep expression or put any other conditions on it: if you have a program whose name is a number, whose name is part of a userβs username, or whose name is a substring of another programβs name (e.g. ssh and sshd), these will all get caught in your grep search, so many processes could get killed inadvertently.
This approach is incredibly unsafe because if you run it with no arguments you will kill every process except grep processes, which will do bad things to your OS.
Process IDs arenβt always 6 digits or fewer: while a 32-bit Linux machine may have up to 32768 processes, 64-bit Linux expands that potential range beyond 6 digits. This means your cut expression may truncate the PID of the actual process youβre looking for.
I would add something like:
Yeah, thanks for the another good advice!
You know checking whether the name exists is a good way not to have any issues with tools like
kill
.It's funny that the example you chose is Slack :D I heard it can become quite a memory hog!
Thanks for the tip!
Honestly I just wanted to check whether it's closed, because
Slack
has an icon in the tray.Usually I am facing issues related to closing processes like
vpn connections
,third-party daemons
, because they don't have icons or GUI, so we can't be sure whether it's closed or not!The problem with daemons is that they are usually controller by the init system (upstart or systemd) generally. So once you kill it, the init system will respawn it. You will have to stop it from the init system