hill2hill3hill4hill5treeleafplant
logo

Dev Capture

A Bash script to kill processes running on a specified port

calendar6/4/2025

play
  • SCRIPT

Overview and Functional Description

  • A Bash script to kill processes running on a specified port
  • Default port is 3000 if no argument provided
  • Checks if port is in use before attempting to kill
  • Uses lsof to find process IDs and kills them with signal 9
  • Provides feedback messages about port status and actions taken
#! /usr/bin/bash

# This script is used to skill port 3000

# take the first argument as the port number
# if no argument is given, use 3000 as default
port=${1:-3000}

# check if the port is in use
lsof -i :$port > /dev/null

# if the port is not in use, exit the script
if [ $? -ne 0 ]; then
    echo "Port $port is not in use"
    exit 0
fi

lsof -i :$port | awk 'NR!=1 {print $2}' | xargs kill -9

echo "Port $port has been killed"