-2

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.

  • 5
    pkill is shorter than superkill tho – llua May 12 '18 at 22:35
  • 1
    alias superkill="pkill -f" – phemmer May 12 '18 at 23:06
  • Also killall (only in Linux!!!) do what you want – Romeo Ninov May 13 '18 at 07:20
  • pkill and killall do not work. i do a ps -A|grep ruby after trying both and the processes persist. manually doing the killall command does, but i can't figure out how to bashify it – user1130176 May 13 '18 at 13:15
  • 1
    @user1130176, why do you need to "bashify" killall (or pkill)? 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 then alias superkill='killall -r' is all you need. – John Bollinger May 13 '18 at 16:44
  • 1
    From the duplicate, An 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 your grep is empty, which is why grep is complaining. – Jeff Schaller May 15 '18 at 00:52
  • 2
    If you indicate that you've read the linked duplicate question and still feel like you want a specific answer to your question, simply edit the Question to say all that; that your Question is not the grep error, but to achieve the kill 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:54
  • 1
    If you're clear enough in your objections to closing this as a duplicate, the Q will enter a "reopen" queue where more well-intended members of the site can discern your intention. – Jeff Schaller May 15 '18 at 00:55
  • Reckless marking of questions as duplicates is self-elevating and does not add value. The linked question does not answer my question, and I still do not have an answer to my question. This is why I hate stack overflow. It rewards trigger happy moderators who like to feel the power of putting other people down to make themselves feel good and leaves people seeking help, like me, with no solution. – user1130176 May 16 '18 at 02:34
  • @JohnBollinger kill all and pkill do NOT work. – user1130176 May 16 '18 at 02:35
  • @user1130176 You mention in a couple of comments that "pkill/killall does not work". Could you elaborate on that? pkill works well here, and I know that killall 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
  • @Kusalananda if i a do a ps -A|grep ruby and then run pkill or run killall, and then do a ps -A|grep, the rubies are still there. however, if i do the manual script ps ax | grep ruby | cut -f1 -d' ' | xargs kill the rubies are gone. i know that killall and pkill SHOULD work but they don't. as for this being SO, its the same platform that incentivizes the same self-elevating policing that SO does so i have the same disdain for this site's flaggers as i do SO's. – user1130176 Jun 12 '18 at 21:07
  • 1
    I'll disregard your blanket dismissal of this site's users and instead I'll ask exactly what your pkill/killall command looked like and also what the ps+grep pipeline that you mention outputs. – Kusalananda Jun 12 '18 at 21:12
  • it's disdain toward the policy that encourages self-elevating snobbish behavior and put-downs of people who seek help, it's not a dismissal of people, it's disdain toward the policy. i am grateful for your help and not dismissing me as stupid or incapable of reading, so thank you for your help in this thread, sincerely. my commands are 'pkill ruby' and 'killall ruby' here's a sample output https://gist.github.com/kevinchugh/8d6ae3eb79267e8ae1bde6655868fc33 – user1130176 Jun 12 '18 at 21:26
  • Please note that all of the suggestions in the duplicates should work as long as you also use the -9 with them. So pkill -9 -f ruby will work as you expect. As will killall -9 ruby. – terdon Jan 06 '19 at 16:33
  • @terdon thanks but it does not work https://i.imgur.com/xc1dT1W.png – user1130176 Jan 06 '19 at 16:41
  • Huh. And neither does killall -9 ruby? – terdon Jan 06 '19 at 16:45

2 Answers2

5

What you're looking for already exists, by the name killall -r:

killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent. [...]

-r, --regexp

Interpret process name pattern as an extended regular expression.

(killall(1) manual page)

4

You can't use positional parameters with an alias.
The command you are running with that alias is:

ps ax | grep | cut -f1 -d' ' | xargs kill something

You need to make this a function.

Much longer than your code but here is a similar function I use:

smash () {
    local T_PROC=$1
    local T_PIDS=($(pgrep -i "$T_PROC"))
    if [[ "${#T_PIDS[@]}" -ge 1 ]]; then
        echo "Found the following processes:"
        for pid in "${T_PIDS[@]}"; do
            echo "$pid" "$(ps -p "$pid" -o comm= | awk -F'/' '{print $NF}')" | column -t
        done
        if ( yorn.ask "Kill them?" ); then
            for pid in "${T_PIDS[@]}"; do
                echo "Killing ${pid}..."
                ( kill -15 "$pid" ) && continue
                sleep 2
                ( kill -2 "$pid" ) && continue
                sleep 2
                ( kill -1 "$pid" ) && continue
                echo "What the hell is this thing?" >&2 && return 1
            done
        else
            echo "Exiting..."
            return 0
        fi
    else
        echo "No processes found for: $1" >&2 && return 1
    fi
}

yorn.ask is a separate function I use:

yorn.ask () {
    read -p "$@ [Y/n]: " RESP && local YORN_RESP="$(echo "${RESP:0:1}" | grep -i "[YN]")"
    while [[ -z "$YORN_RESP" ]]; do
        echo "Please respond only with: y or n"
        read -p "$@ [Y/n]: " RESP && local YORN_RESP="$(echo "${RESP:0:1}" | grep -i "[YN]")"
    done
    [[ "$YORN_RESP" == 'Y' || "$YORN_RESP" == 'y' ]] && return 0 || return 1
}
jesse_b
  • 37,005