I have a simple TCP server that listens on a port.
var net = require(\"net\");
var server = net.createServer(function(socket) {
socket.end(\"Hello!\\n\"
You can use fuser to get what you want to be done.
In order to obtain the process ids of the tasks running on a port you can do:
fuser <>/tcp
Let's say the port is 8888, the command becomes:
fuser 8888/tcp
And to kill a process that is running on a port, simply add -k switch.
fuser <>/tcp -k
Example (port is 8888):
fuser 8888/tcp -k
That's it! It will close the process listening on the port. I usually do this before running my server application.