Created
January 14, 2024 07:25
-
-
Save arielivandiaz/1caeef6c29b6a09d3a83e33f42b4e54b to your computer and use it in GitHub Desktop.
kill-process-by-port.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if a port number was provided | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 port_number" | |
exit 1 | |
fi | |
# Assign the first argument to a variable | |
PORT=$1 | |
# Find the process that is using the port | |
PID=$(sudo lsof -t -i:$PORT) | |
# Check if a PID was found | |
if [ -z "$PID" ]; then | |
echo "No process found using port $PORT." | |
exit 1 | |
else | |
echo "Killing process with PID $PID that is using port $PORT." | |
sudo kill $PID | |
# Check if the kill command was successful | |
if [ $? -eq 0 ]; then | |
echo "Process terminated successfully." | |
else | |
echo "Error trying to terminate process $PID." | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment