# PID helper
# get PID from a tcp PORT number
function pid_from_port(){
if [ "$1" ]
then
# list all process related to the given port
# lsof -P | grep ':$1' | awk '{print $2}'
lsof -n -iTCP:$1 -sTCP:LISTEN -n -l -P | grep 'LISTEN' | awk '{print $2}'
else
echo "no port defined"
fi
}
# kill process related to a given port
function freeport(){
if [ "$1" ]
then
pid_from_port $1 | xargs kill -9
else
echo "no port defined"
fi
}
example of use:
pid_from_port # output: no port defined
pid_from_port 3000 # output: <PID>
freeport # output: no port defined
freeport 3000 # output: none and PID killed
pid_from_port # output: no port defined
pid_from_port 3000 # output: <PID>
freeport # output: no port defined
freeport 3000 # output: none and PID killed
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.