Kill a Process by Port

- - posted in ruby | Comments

Kill a process by port

1
fuser -k 9000/tcp

This replaces manually grepping for process id and killing it:

1
2
ps aux | grep unicorn
kill -9 PID

Note: This does not work on MacOS.

Example usage:

1
2
3
4
5
6
7
#!/bin/sh
echo "Restarting Unicorn on port 9000"
fuser -k 9000/tcp
while nc -vz localhost 9000; do sleep 1; done
bundle exec unicorn_rails -p 9000 -E production -D
while ! nc -vz localhost 9000; do sleep 1; done
curl http://localhost:9000

Comments