Skip to content

Instantly share code, notes, and snippets.

@benshimmin
Created August 17, 2012 16:00
Show Gist options
  • Save benshimmin/3380146 to your computer and use it in GitHub Desktop.
Save benshimmin/3380146 to your computer and use it in GitHub Desktop.
Search for a process called {searchterm} and kill it if it exists
#!/bin/bash
# The secret is to use the word boundary in the regex so it doesn't
# return the grep process too.
# Then use awk to extricate the second column of the match.
# Capture that as $PID and kill -9 it (if it exists).
PID=`ps auxww | grep -E "\\bsearchterm" | awk '{ print $2 }'`
if [ $PID ]; then
kill -9 $PID
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment