We need to change the grep slightly, to stop the actual grep command process that we just ran from returning. We can add [] around part of the matching string to stop the process from being matched exactly as it will be parsed as a pattern, and not a literal string.

It doesn’t matter where on the string you add the brackets, as long as they are there somewhere.

Using awk we can now filter the results line to only print out the pid of the mongodb process.

1

2

psaux|grepmong[o]|awk{'print$2'}

1025

Finally, we need to wrap all of this with the kill statement to remove the process. Be careful here as this will immediately kill the process with no warning or confirmation. This is just an example, it’s never a good idea to forcefully kill the mongodb process!