There are many way to do this but let's focus on the main two that I know:
Using ps and xargs
With grep:
ps -ef | grep '<pattern>' | grep -v grep | awk '{print $2}' | xargs -r kill -9
With awk:
ps -ef | awk '<pattern>'/<pattern>/ && !/awk/ {print $2}' | xargs -r kill -9
These commands will kill all the process that match with the <pattern>
So your script will be like:
#!/bin/bash
ps -ef | grep 'gunicorn' | grep -v grep | awk '{print $2}' | xargs -r kill -9
[ $? -eq 0 ] && echo 'killed gunicorn'
Or,
#!/bin/bash
ps -ef | awk '/gunicorn/ && !/awk/ {print $2}' | xargs -r kill -9
[ $? -eq 0 ] && echo 'killed gunicorn'
Using ps and for
for pid in `ps ax | grep '<pattern>' | awk ' { print $1;}'`; do
kill -9 $pid
done
This also will kill all the process that match with the <pattern>
So your script will be like:
#!/bin/bash
for pid in `ps ax | grep 'gunicorn' | awk ' { print $1;}'`; do
kill -9 $pid
done
[ $? -eq 0 ] && echo 'killed gunicorn'