4

I want to trigger a script when port 3000 is opened by a process. The process is a Rails development server, and I just want to be notified when it finishes spinning up and port 3000 starts accepting requests.

4

Here is a oneliner that simply loops until the port is open. This is quick and dirty, but it might get you started.

while ! nc -z localhost 3000; do sleep 0.1; done; echo 'The server is up!'

Replace the echo statement with your action of choice.

  • nc is Netcat, "the Swiss-army knife for TCP/IP",
  • -z means: do not send any data, just check if the port is open,
  • while ! nc -z …; do sleep 0.1; done: keep checking and sleeping for one tenth of a second until the port opens up, i.e. Netcat returns with a zero (success) status.

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.