0

Ubuntu 16.04

I am used to using this command line to kill any process match said COMMAND

for i in $(ps -ef | grep COMMAND | awk '{ print $2 }'); do kill -9 "$i" &>/dev/null;done

shellcheck does not like it much and suggests

root@singa /home # shellcheck run

In run line 4:
for i in $(ps -ef | grep COMMAND | awk '{ print $2 }'); do kill -9 "$i" &>/dev/null;done
           ^-- SC2009: Consider using pgrep instead of grepping ps output.

what are the flags needed to complete this here replacing the XXX

for i in $(pgrep XXX | grep COMMAND | awk '{ print $2 }'); do kill -9 "$i" &>/dev/null;done

I have already checked the man page for pgrep.

  • How about just using pkill ? linux.die.net/man/1/pkill – steve Jun 14 '18 at 21:38
  • Did you even try just running pgrep COMMAND before trying to drop it in the place of ps in your script? That should make it clear what has hone wrong with what you did. – DopeGhoti Jun 14 '18 at 21:51
  • @DopeGhoti I did not. You are correct and I should have. – Curious Sam Jun 14 '18 at 22:01
1
for pid in $(pgrep command); do
    kill -9 $pid
done

Or, better yet:

pkill -9 command

As a side note, disinterested use of kill -9 is known to sometimes be the cause of zombie outbreaks, orphaned files, permanent temporary files, and other maladies. Please instead consider just using kill which by default sends SIGTERM rather than SIGKILL.

| improve this answer | |
  • The for i pid command worked well. Appreciated. – Curious Sam Jun 14 '18 at 22:02
0

i have done below method

for i in pickup hald-runner; do pgrep $i | awk '{print "kill" " "  $1}'| sh;done

Here pickup,hald-runner is the process names

| improve this answer | |

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.