So you have started your django or Node JS or Ruby on Rails development server. Then you make some code changes that require the dev server reboot. Usually the server automatically watches your code files and reboot by itself, but sometimes, It doesn't. You have to stop or restart the server process manually.

Usually the devp server daemon will tell you "Quit the server with CONTROL-C". But we all know this doesn't work.

#python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 10, 2018 - 14:20:33
Django version 2.0, using settings 'sitemonkey.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: That port is already in use.

The long solution is to look for process ID or PID of the server listening on whatever port it's running such as 8000. You can do this by running netstat or lsof or ss. Get the PID and then run the kill command.

But you can achieve the same using a single one-liner;

sudo kill $(lsof -t -i:8000) 
# or 
sudo fuser -k -n tcp 8000 
# or 
fuser -k 8000/tcp

That's it. Now you can run your dev server again and it won't complain that "port is already in use". Quick note sudo lsof -ni :{port} lists processes that are currently running under a specific port.

If fuser -k {port}/tcp seems like a long command to you, then you can add a function that takes the port as argument in ~/.bashrc or ~/.zshrc depending on your shell. You can then simply call kill_port {port}.

#######kill_port##############
kill_port(){
    if [ $# -eq 0 ]; then
        echo "No arguments provided"
        echo "provide port of service you wish to kill"
        exit 1
    fi
    fuser -k $1/tcp
}

Oh, it's 2019 and I started a cool project that automates website uptime monitoring called Site Monki I would love you to checkout if you want to keep tabs on your app uptime.