Using grep
on the results of ps
is a bad idea in a script, since some proportion of the time it will also match the grep process you've just invoked. The command pgrep
avoids this problem, so if you need to know the process ID, that's a better option. (Note that, of course, there may be many processes matched.)
However, in your example, you could just use the similar command pkill
to kill all matching processes:
pkill ruby
Incidentally, you should be aware that using -9
is overkill (ho ho) in almost every case - there's some useful advice about that in the text of the "Useless Use of kill -9
form letter ":
No no no. Don't use
kill -9
.It doesn't give the process a chance to cleanly:
- shut down socket connections
- clean up temp files
- inform its children that it is going away
- reset its terminal characteristics
and so on and so on and so on.
Generally, send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved!
Don't use
kill -9
. Don't bring out the combine harvester just to tidy up the flower pot.