shell script to kill the process listening on port 3000? [duplicate]

Solution 1:

alias kill3000="fuser -k -n tcp 3000"

Solution 2:

Try this:

kill -9 $(lsof -i:3000 -t)

The -t flag is what you want: it displays PID, and nothing else.

Update

In case the process is not found and you don't want to see error message:

kill -9 $(lsof -i:3000 -t) 2> /dev/null

Assuming you are running bash.

Update

Basile's suggestion is excellent: we should first try to terminate the process normally will kill -TERM, if failed, then kill -KILL (AKA kill -9):

pid=$(lsof -i:3000 -t); kill -TERM $pid || kill -KILL $pid

You might want to make this a bash function.