DEV Community

Cover image for Killing many procs at one time
Dmitry
Dmitry

Posted on

Killing many procs at one time

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
Enter fullscreen mode Exit fullscreen mode

Once the file is created we have to make it executable, easily:

chmod +x ./kill-all.sh
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
ferricoxide profile image
Thomas H Jones II

How does what you're doing, above, differentiate from just using pkill and related tools?

Collapse
 
dskuratovich profile image
Dmitry • Edited

That's a good question and the answer would be nothing, so pkill 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 ยฏ_(ใƒ„)_/ยฏ

Collapse
 
ferricoxide profile image
Thomas H Jones II

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. =)

Thread Thread
 
dskuratovich profile image
Dmitry

No, you mayn't have to worry, they are the same things!

Collapse
 
ikirker profile image
Ian Kirker

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.

Collapse
 
mrm8488 profile image
Manuel Romero

I would add something like:

if [ -z $1 ]
then
 echo "You must pass the process name as a parameter"
 exit 1
fi
Collapse
 
dskuratovich profile image
Dmitry

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.

Collapse
 
rhymes profile image
rhymes

It's funny that the example you chose is Slack :D I heard it can become quite a memory hog!

Thanks for the tip!

Collapse
 
dskuratovich profile image
Dmitry

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!

Collapse
 
andreanidouglas profile image
Douglas R Andreani

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

systemctl stop sshd.service