-1

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.

marked as duplicate by G-Man, Patrick, Jeff Schaller, Satō Katsura, Anthony Geoghegan May 13 '18 at 23:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    pkill is shorter than superkill tho – llua May 12 '18 at 22:35
  • 1
    alias superkill="pkill -f" – Patrick May 12 '18 at 23:06
  • 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 0:52
  • 1
    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 0:54
4

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)

  • killall does not work – user1130176 May 13 '18 at 13:15
3

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
}
  • What isyorn ? – iruvar May 12 '18 at 22:30
  • @iruvar: sorry -- updated – Jesse_b May 12 '18 at 22:31
  • smash looks like what i want but i tried it and it does not kill the ruby processes. the only thing that works is ps ax | grep ruby | cut -f1 -d' ' | xargs kill – user1130176 May 13 '18 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.