-1

I'm trying to write an alias that will kill all running php processes.. this is the command i got in my ~/.bash_profile

alias killphp="ps aux | g [p]hp | awk '{print $3}' | xargs kill -9"

this command works just fine if i type it as is without the alias.. but if i simply run killphp I get this weirdness:

grep: warning: recursive search of stdin
kill: illegal process id: (standard
kill: illegal process id: input):2:abdullah
kill: illegal process id: 10.9
kill: illegal process id: 0.2
kill: 2496576: No such process
kill: 20460: No such process
kill: illegal process id: ??
kill: illegal process id: S
kill: illegal process id: 7:14AM
kill: illegal process id: 0:00.28
kill: illegal process id: /usr/local/Cellar/php54/5.4.28/bin/php
kill: illegal process id: /Users/abdullah/dev/php/smartTaxi/app/console
kill: illegal process id: bcc:resque:worker-start
kill: illegal process id: -f
kill: illegal process id: request_queue
kill: illegal process id: --env=dev
kill: illegal process id: (standard
kill: illegal process id: input):3:abdullah
kill: illegal process id: 6.0
kill: illegal process id: 0.1
kill: 2486972: No such process
kill: 9708: No such process
kill: illegal process id: ??
kill: illegal process id: S
kill: illegal process id: 7:14AM
kill: illegal process id: 0:00.08
kill: illegal process id: php
kill: illegal process id: /Users/abdullah/dev/php/smartTaxi/app/../vendor/chrisboulton/php-resque/resque.php

if I run type killphp I get this:

killphp is aliased to `ps aux | g [p]hp | awk '{print }' | xargs kill -9'

so apparently the $3 argument to awk is gone! how do I escape it? I tried doing /$3 and $$3 but none of them works.

3

You'd be much better off using pkill. But to address your question, make killphp a bash function instead of an alias, by putting this into your profile.

killphp() {
  ps aux | g [p]hp | awk '{print $3}' | xargs kill -9
}

You can then use it just like the alias:

killphp

You could have escaped the $ by writing \$, but with functions you don't have to worry about quoting.

  • questions: 1) why is pkill better than just kill? 2) how do i run a shell function from command line? – abbood Jun 27 '14 at 4:29
  • ah i see how pkill is better.. it saves me all the hassle of awking and grepping.. nice – abbood Jun 27 '14 at 4:31
  • @abbood: because you call pkill with the (p)attern: pkill php. No messing around with ps+grep etc. Answered the other question in the answer. – rici Jun 27 '14 at 4:31
  • @abbood: The pattern is a regex, but partial matches are fine so pkill supervisor should work. It works for me, anyway. – rici Jun 27 '14 at 4:40
  • you are right.. i was looking at the wrong thing lol :) – abbood Jun 27 '14 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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