I am trying to make a superkill alias that kills all process that hit a grep match. I'd like to do:
superkill ruby
And have it kill all processes that match 'ruby'
In my .bashrc, I've added this
alias superkill="ps ax | grep $1 | cut -f1 -d' ' | xargs kill"
However, when I run it, I get
superkill something
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
...
Can someone point me in the right direction?
Thanks, Kevin
This is different than the duplicate. I am asking a specific question. The duplicate is a high level question that speaks in generalities. I am unable to come up with a solution after reading the duplicate.
pkill -f
does not work. if you do
ps -A|grep ruby
and then
pkill -f ruby
and then
ps -A|grep ruby
You will see that a ruby processes still exist. BUT if you do
kill -9 xxx
where xxx is the PID, this does work. What I'm looking for is something that aliases kill -9 to the matches to grep.
UPDATE - this reminds me of my frustration with SO, where everyone is too quick to downvote or flag (i contend that flagging has very little value). Here's yet another example where all of the suggested answers do not work:
kevin@kpi-2:~/Documents/code/kpi-veh$ sudo killall -r ruby
kevin@kpi-2:~/Documents/code/kpi-veh$ ps -A|grep ruby
27175 pts/9 00:22:03 ruby
kevin@kpi-2:~/Documents/code/kpi-veh$ sudo killall -r ruby
kevin@kpi-2:~/Documents/code/kpi-veh$ ps -A|grep ruby
27175 pts/9 00:22:06 ruby
kevin@kpi-2:~/Documents/code/kpi-veh$ sudo killall -r ruby
kevin@kpi-2:~/Documents/code/kpi-veh$ ps -A|grep ruby
27175 pts/9 00:22:08 ruby
kevin@kpi-2:~/Documents/code/kpi-veh$ kill -9 27175
kevin@kpi-2:~/Documents/code/kpi-veh$
what i am looking for (cannot believe i still have to argue with the hall monitors that this is a dupe) is a way to alias kill. In this case (this happens often with Ruby), I HAVE to resort to killing the process by ID, killall in any variant does not work, as best I can tell.
alias superkill="pkill -f"
– phemmer May 12 '18 at 23:06killall
(only in Linux!!!) do what you want – Romeo Ninov May 13 '18 at 07:20killall
(orpkill
)? You can just use it directly to serve the purpose you describe! Or if you want to avoid having to manually provide the-r
option thenalias superkill='killall -r'
is all you need. – John Bollinger May 13 '18 at 16:44An alias should effectively not (in general) do more than change the default options of a command. It is nothing more than simple text replacement on the command name. It can't do anything with arguments but pass them to the command it actually runs. So if you simply need to add an argument at the front of a single command, an alias will work.
That's a long way of saying "you can't use $1 in aliases". You'd have to read between the lines just a bit to realize that$1
for yourgrep
is empty, which is why grep is complaining. – Jeff Schaller May 15 '18 at 00:52grep
error, but to achieve thekill
behavior your want. Ranting against well-intended members of the site is not a good way to use the site. – Jeff Schaller May 15 '18 at 00:54pkill
works well here, and I know thatkillall
work too, so there must be something in your usage of these utilities that is making them "not work". Also, this is not StackOverflow. – Kusalananda Jun 12 '18 at 20:28-9
with them. Sopkill -9 -f ruby
will work as you expect. As willkillall -9 ruby
. – terdon Jan 06 '19 at 16:33killall -9 ruby
? – terdon Jan 06 '19 at 16:45