nixos/modules/scripts/port-kill.bash
Nickolaj Jepsen 575db8e773
Some checks failed
CI / fmt (push) Failing after 2s
CI / check (push) Failing after 2s
feat: add more scripts
2026-01-22 23:12:42 +01:00

25 lines
478 B
Bash

#!/usr/bin/env bash
set -euo pipefail
PORT=$1
if [ -z "$PORT" ]; then
echo "Usage: port-kill <port>"
exit 1
fi
# lsof returns exit code 1 if no files found
PID=$(lsof -t -i:"$PORT" || true)
if [ -z "$PID" ]; then
echo "No process found on port $PORT"
exit 1
fi
COMMAND=$(ps -p "$PID" -o comm=)
echo "Process '$COMMAND' (PID $PID) is using port $PORT."
read -p "Kill? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
kill "$PID"
echo "Killed."
fi