Kill Multiple Processes By Single Command in Linux 0 1063

Kill Multiple Processes By Single Command in Linux 0 1064

There are the cases where we have seen there are many processes are running and we would like to kill them all. It will be tedious work to do.

Let see how we can get such concern overcome.

  1. Check the processes
  2. Type ps -ef command

    root 5747 1 0 Aug28 ? 00:00:34 sendmail: accepting connections
    root 10609 12566 0 00:51 ? 00:00:00 sshd: root@pts/0
    root 10644 10609 0 00:51 pts/0 00:00:00 -bash
    root 12532 1 0 May14 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vsf
    root 12566 1 0 May14 ? 00:01:10 /usr/sbin/sshd
    root 13797 2643 0 Aug29 ? 00:02:25 smbd -D
    root 14606 2 0 Sep08 ? 00:00:00 [scsi_eh_59]
    root 14607 2 0 Sep08 ? 00:00:00 [usb-storage]
    root 14646 2 0 Sep08 ? 00:00:00 [kjournald]
    root 14670 2 0 Sep08 ? 00:00:00 [kjournald]
    root 14675 2 0 Sep08 ? 00:00:00 [kjournald]
    root 15250 1542 0 Aug05 ? 00:00:00 CROND
    root 17835 10644 1 01:56 pts/0 00:00:00 ps -ef
    root 20723 2643 0 Aug17 ? 00:00:42 smbd -D
    root 22497 1 0 Feb22 ? 00:20:59 /sbin/rsyslogd -c 4
    root 27565 2643 0 Sep06 ? 00:03:51 smbd -D
    root 27773 2 0 Sep19 ? 00:00:00 [flush-253:1]

  3.  Check your processes that you are interested in kill.
  4.  Suppose you want to kill all the invalid instances of “sshd” that might created mistakenly.
  5.  What will you do? Any idea, think little.  Well first we need to see how many “sshd” instances are running on our server. So the best command to use is “grep” it first.

Command: ps -ef | grep sshd

Output:

root 29245 25324 0 02:06 ? 00:00:00 sshd: rohit [priv]
sshd 29247 29245 0 02:06 ? 00:00:00 sshd: rohit [net]
root 29316 25324 0 02:07 ? 00:00:00 sshd: rohit [priv]
sshd 29317 29316 0 02:07 ? 00:00:00 sshd: rohit [net]
root 29326 25324 0 02:07 ? 00:00:00 sshd: rohit [priv]
sshd 29327 29326 0 02:07 ? 00:00:00 sshd: rohit [net]
root 29434 25324 0 02:08 ? 00:00:00 sshd: rohit [priv]
sshd 29435 29434 0 02:08 ? 00:00:00 sshd: rohit [net]
root 29443 25324 0 02:08 ? 00:00:00 sshd: root@pts/1
root 29457 25324 1 02:08 ? 00:00:00 sshd: rohit [priv]
sshd 29458 29457 0 02:08 ? 00:00:00 sshd: rohit [net]
root 29460 25324 1 02:08 ? 00:00:00 sshd: rohit [priv]
sshd 29461 29460 0 02:08 ? 00:00:00 sshd: rohit [net]
root 29462 25324 0 02:08 ? 00:00:00 sshd: rohit [priv]
sshd 29463 29462 0 02:08 ? 00:00:00 sshd: rohit [net]
root 29469 25324 5 02:08 ? 00:00:00 sshd: rohit [priv]
sshd 29470 29469 1 02:08 ? 00:00:00 sshd: rohit [net]
root 29471 25324 0 02:08 ? 00:00:00 sshd: rohit [priv]
sshd 29472 29471 1 02:08 ? 00:00:00 sshd: rohit [net]
root 29475 25324 1 02:08 ? 00:00:00 sshd: [accepted]
sshd 29476 29475 0 02:08 ? 00:00:00 sshd: [net]

We can see on the second item is the process id, and to kill any process we need its process ID or processes IDS.

Here we see there are the sshd instances created by the username rohit, which is really many. Yes we want to kill them off.

Select the processes with specific rohit user:

Command: ps -ef |grep “rohit”  or ps -ef |grep rohit

The above command will give you the process related with user rohit or any process with name rohit.  You must remember to replace it with your identifier or keyword that you want to look into process list.

But that was not our objective, we want to kill them off.

Have you heard about AWK, well we will not take this in this tutorial but will discuss separately on separate post. Subscribe with us may help you to be in touch and get email of various post we are posting on our site.

We know the command “kill” only takes process ids.

Let get the process ids

Command: ps -ef | grep “rohit” | awk ‘{print $2}’ 

Process ID Selection To kill Multipl Processes
Command to Select Print Process ID on Shell Console.

Only one more command to go, to kill all these process IDs.

Command:

kill -9 `ps -ef | grep rohit |  awk ‘{print $2}’`

Or

Alternate Command:

kill -9 `ps -ef | grep rohit | grep -v grep | awk ‘{print $2}’`

Note: Alternate command is to verify the output of the grep and which exist kill only that.

So thats it? Yeah thats it.

Make your system to work more efficiently and keep eye on the processes that may hang up and use resources without your permission.

Previous ArticleNext Article
Hey guys, thanks for reading our posts. We are collecting news and information that required our attention and make us to thinks upon it. We make our blogs as much as informatory to support our needs. Hope we will be encouraged and have your comments and thoughts on maximum posts.

Send this to a friend