Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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