If you have ever wanted to kill a server process from a Makefile so that the fresh command can be run without having to manually kill the old server, you can do it using the lsof
command to find the PID of the process running on a specific port and then kill it.
Here’s a simple example of how to do this in a Makefile:
SERVER_PORT=8022
kill-server:
@PID=$(shell lsof -t -i :$(SERVER_PORT)) && \
if [ ! -z "$$PID" ]; then echo "killing process with PID $$PID"; kill -9 $$PID; fi
dev:
@make kill-server
go run .