How to Check Port Availability and Kill Processes in Linux
- Category Shell
- Type Command
- Platform Linux
- Language Bash
- Price Free
- Copy 1 658
- Comments 0
How to Check Port Availability and Kill Processes in Linux
#!/usr/bin/env bash
# Check if a specific port is in use and forcefully kill the process
# Usage: ./kill-port.sh [port_number]
PORT="${1:-8080}"
PID=$(lsof -t -i:"$PORT")
if [ -z "$PID" ]; then
echo "Port $PORT is free."
else
echo "Killing process $PID on port $PORT..."
kill -9 "$PID"
fi
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.
There are no comments yet :(