Bash script to check if MongoDB is running on a certain port

1,982 views
Skip to first unread message

Phuc Nguyen

unread,
Jun 22, 2014, 3:58:07 AM6/22/14
to meteo...@googlegroups.com
I have two separate apps which use the same database. Normally I start app 1 first, wait for the database to be ready, then start app 2. The MONGO_URL of app 2 is set to "mongodb://127.0.0.1:3001/meteor".

I want to write a bash script which will automate the above process. My question: How can we check if MongoDB is started on port 3001 and is ready to accept connections?

Arunoda Susiripala

unread,
Jun 22, 2014, 4:05:58 AM6/22/14
to meteo...@googlegroups.com
Use this: https://www.npmjs.org/package/wait-for-mongo

This is something I build for Meteor Up
On Sun, Jun 22, 2014 at 1:28 PM, Phuc Nguyen <ma...@nguyenphuc.info> wrote:
I have two separate apps which use the same database. Normally I start app 1 first, wait for the database to be ready, then start app 2. The MONGO_URL of app 2 is set to "mongodb://127.0.0.1:3001/meteor".

I want to write a bash script which will automate the above process. My question: How can we check if MongoDB is started on port 3001 and is ready to accept connections?

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Phuc Nguyen

unread,
Jun 22, 2014, 2:02:48 PM6/22/14
to meteo...@googlegroups.com
That's what I'm looking for. Thanks a lot, Arunoda.

Roman Zolotarev

unread,
Aug 21, 2014, 12:55:19 AM8/21/14
to meteo...@googlegroups.com
Hi Arunoda,
 
Is there a way to run it inside the application?
Ideally I'd like to have a node process that trying to connect to MondoDB until its loading (on a new server it takes longer).
Thanks!

Arunoda Susiripala

unread,
Aug 21, 2014, 12:58:05 AM8/21/14
to meteo...@googlegroups.com

--

Roman Zolotarev

unread,
Aug 21, 2014, 3:17:57 AM8/21/14
to meteo...@googlegroups.com
I've added this to Meteor app, but this didn't help.

# server/startup.coffee

Meteor.startup  ->

  waitForMongo = require 'wait-for-mongo'
  waitForMongo 'mongodb://127.0.0.1:27017/meteor',
    timeout: 1000 * 60 * 5
  , (error) ->
    if error
      console.log "Can not connect to MonogoDB."
    else
      console.log "MongoDB comes online."


On Meteor app start while mongod is DOWN, I'm getting this:

2014-08-21_07:09:38.76568 Exception in callback of async function: Error: failed to connect to [127.0.0.1:27017]
2014-08-21_07:09:38.76576     at null.<anonymous> (/opt/app/programs/server/npm/mongo-livedata/main/node_modules/mongodb/lib/mongodb/connection/server.js:536:74)
2014-08-21_07:09:38.76577     at emit (events.js:106:17)
2014-08-21_07:09:38.76578     at null.<anonymous> (/opt/app/programs/server/npm/mongo-livedata/main/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
2014-08-21_07:09:38.76579     at emit (events.js:98:17)
2014-08-21_07:09:38.76580     at Socket.<anonymous> (/opt/app/programs/server/npm/mongo-livedata/main/node_modules/mongodb/lib/mongodb/connection/connection.js:516:10)
2014-08-21_07:09:38.76581     at Socket.emit (events.js:95:17)
2014-08-21_07:09:38.76582     at net.js:440:14
2014-08-21_07:09:38.76582     at process._tickCallback (node.js:419:13)


On Meteor app start while mongod is UP, I'm getting this:

2014-08-21_07:16:01.24517 MongoDB comes online.

Arunoda Susiripala

unread,
Aug 21, 2014, 3:56:09 AM8/21/14
to meteo...@googlegroups.com
If don't know what require method does. Seems like a async wrapper. 

--

Roman Zolotarev

unread,
Aug 21, 2014, 4:08:15 AM8/21/14
to meteo...@googlegroups.com
Dear Arunoda, could you please show an example how to use it inside the app?

Here is my startup.coffee

Roman Zolotarev

unread,
Aug 21, 2014, 4:09:52 AM8/21/14
to meteo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages