I'm writing a script that will spin up server that want to use certain port number. In some occasions this port is occupied. I want to know how to check it in bash so I can react in proper way.
How can I check on linux certain port is occupied?
I'm writing a script that will spin up server that want to use certain port number. In some occasions this port is occupied. I want to know how to check it in bash so I can react in proper way.
How can I check on linux certain port is occupied?
You can use netcat to check if port is listening something:
This of course only works if there isn't firewall blocking netcat. But if you run it locally on the server then at least there shouldn't be any problems.
If you have in mind TCP port, you can check with netstat, something like this might work:
Of course this is not atomic, so it possible that some port will taken after check operation but before your service will start.