I'm using Ubuntu 16.04 servers. Each server hosts multiple services.

Sometimes, when a service is restarted, it fails to bind to the port and says that the port is already in used. Indeed, some system service (or some other service which downloads tons of stuff from multiple websites) might be using the port at that particular moment.

So I retry again and again to bind to the port until it finally succeeds, but this can take some time.

How can I prevent that from happening ? How can I make sure a port is not in used while my service is not running ?


  • 3
    Do your services bind to random ports? Or how come listener port becomes busy? My gutfeeling says it's your process that's just too slow to die and systemd script isn't configured well enough to wait for it to gracefully die. – favoretti Nov 28 '18 at 20:14
  • I agree with @favoretti. Try to check what is it that is making the port busy, use sudo netstat -latnp and you should know which process is holding that port – Jorge Valentini Nov 28 '18 at 21:19

option 1 : tune /proc/sys/net/ipv4/ip_local_port_range to make sure the port you want your app to listen to is outside the range

option 2 : use 1 ip per service you are hosting, and ensure that your application actually binds its ip. this one is ok if you don't have a lot of services concerned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.