I'm trying to kill any process that has to do with mysql. I'm piecing together a command, and so far, haven't come up with the right solution. One other consideration, is that the last process will correspond to the command 'grep mysql' - part of the command below. This process won't exist anymore by the time I try to kill it, so it needs to be excluded from the input piped to kill -9. Whats the best way to go about accomplishing this task? So far, I have the incomplete command:

ps -A | grep mysql | awk '{print $1}'

which has the (fake, for the purpose of this example) output

33333  # process id to be excluded

I was assuming I could pipe that to xargs kill -9 like so:

ps -A | grep mysql | awk '{print $1}' | xargs kill -9

and figure out a way to exclude the last pid, but everything I try does not seem to work correctly.

  • 3
    Never use kill -9 with mysql!! Use kill -15 instead, or you'll risk to corrupt your datafiles. Try to not use kill -9 at all!! – dAm2K Mar 18 '12 at 22:53

Probably the easiest thing to do here is to make grep not match its own command:

ps -A | grep [m]ysql | awk '{print $1}' | xargs kill -9

The brackets around the m make it a character set that only includes m, which doesn't change the pattern but won't match itself in the ps output. This is sort of the canonical way to avoid this issue. I'd also like to add that if you have the pkill command that running pkill -9 mysql is a bit easier.

It's also worth noting that you should avoid using SIGKILL (-9) when possible, as it does not give the target process a chance to exit gracefully. You should probably try with SIGTERM (default) first if you have not already.

| improve this answer | |
  • 1
    First thing: I do not have pkill installed. Another purpose of this was to start to utilize awk since I'm relatively new to it which is why I didn't just install pkill and use that. Second thing, is the [m]ysql tip worked as expected. I'll keep that one in mind for the future. However, it still did not kill the processes. I get no error output after running the command, but the processes are still running. If i just do a regular kill -9 11111, they are both successfully killed. Any ideas? – well actually Mar 15 '12 at 17:35
  • @Charlotte It ought to work (I've done very similar commands myself before). Does it work if you manually put them all on one line? Be sure to use the external kill command ("which kill" to find out where it is) explicitly, as that's what xargs will be calling. – FatalError Mar 15 '12 at 18:41
  • If you are on Linux, you can also achieve the same goal of not matching grep by using ps's -C switch to match by process name, and -N to negate the match. ps -N -C grep | grep mysql | ... – nitro2k01 Nov 25 '13 at 13:49
  • For me it didn't work as expected in ubuntu 14.04. I had to put the pattern that follows grep within quotes. – sfotiadis Nov 19 '14 at 17:37

Awk has a system() command that allows you to kill it directly ... Something like this.

ps ¦awk '/[m]ysql/{system( "kill -9 "$1)}'

The reason for the square brackets is similar to the previous. It prevents awk from being killed and closing the pipe.

The /string/ will perform grep-like line matching.

| improve this answer | |

For completeness I would also suggest the commands pgrep and pkill.

  • pgrep [-lf] [-u uid] [-other options] pattern list processes matching the extended regular expression pattern
  • pkill [-f] [-t**SIGNAL] pattern kill processes matching pattern

The most interesting options, as indicated are probably

  • -f match the complete command line of a process
  • -u user only match a particular users processes (also, groups...)
  • -l list the processes name/identifier (only pgrep)

For the others, see the fine manual.

All in all, these are much more versatile and usefull (in terms of beeing able to actually check what you kill before you do) than killall or the more complicated pipe recipes posted above.

| improve this answer | |

To remove the last one from the list of PID's, you can use head -n N, where n is the number of PID's without the last one.

ps -A | grep mysql | awk '{print $1}' | head -n 2 | xargs kill -9

The assumption is that u know the number of PID that will be piped from grep.

| improve this answer | |
  • This is an answer of dubious quality to a sideline question to the main question. (1) FatalError’s answer is better than any other I’ve seen. (2) Your answer implicitly supports the OP’s flawed notion that the grep itself will always be the last process listed. (3) It seems unlikely that the user will know the number of processes in advance. (4) If you’re going to cite head -n, you should mention that, on some systems, head -n -1 will output all but the last line. – Scott Mar 8 '13 at 1:10

Or the

killall mysql 

command might do it for you. It uses names instead of PIDs.

| improve this answer | |
kill -9 `ps aux | grep PROCESS_NAME | grep -v grep  |  cut -d' ' -f6`
| improve this answer | |
  • 1
    Even if the question is old, please detail how your one-liner works. Plus i'd recommend using $()instead of backquotes. And use awk instead of cut. And don't use kill -9 unless anything else has failed. – mveroone Nov 25 '13 at 14:28

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.