Skip to content

Instantly share code, notes, and snippets.

@fitorec
Last active September 3, 2022 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitorec/9068f30a2088465df9acc65fb7cb1688 to your computer and use it in GitHub Desktop.
Save fitorec/9068f30a2088465df9acc65fb7cb1688 to your computer and use it in GitHub Desktop.
Stopping service on specific port
#!/bin/bash
#
#
# PROBLEM:
# ---------------------
# If we run:
# pkill gunicorn
# We stop all gunicorn services, in this case to start gunicorn we only need to stop the parent
# process associated with the service that attends the port where gunicorn will be executed.
#
# The following script searches for said process (pid), if it exists it kills this process:
stop_unicorn_on_port() {
pid=$(lsof -w -t -i "TCP:${1}" | head -1)
if [ -z "${pid}" ]; then
echo "🦄 no service deamon on port ${1}"
else
kill -9 "${pid}"
echo "🦄 killed service deamon(${pid}) on port ${1}"
fi
}
# Example/Testing
stop_unicorn_on_port 5000
stop_unicorn_on_port 5001
stop_unicorn_on_port 5002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment