371273
371273

Reputation: 5436

Using PM2 to run a complete development environment

I'm using PM2 to run my Node microservices, and in development I would also like to start MongoDB so that I can be up and running for development with a single command. I have a development ecosystem.js file that looks something like this:

apps: [
        {
            name: 'API',
            script: 'index.js',
            out_file: './logs/api.stdout.log',
            error_file: './logs/api.stderr.log'
        },
        {
            name: 'Emailer',
            script: 'services/emailer/index.js',
            out_file: './logs/emailer.stdout.log',
            error_file: './logs/emailer.stderr.log'
        },
        {
            name: 'MongoDB',
            script: 'services/development/mongodb.sh'
        }
    ]

and mongodb.sh looks like this:

#!/bin/bash
mongod

When I start PM2, everything runs just fine. When I call pm2 stop all, it kills the node processes, but doesn't kill MongoDB.

I'm guessing that this is because it's attaching to the bash instance, not the mongod instance. Is there a way to make PM2 start and attach to an executable, rather than a script file?

Upvotes: 1

Views: 3799

Answers (1)

İlker Korkut
İlker Korkut

Reputation: 3250

As you know;

PM2 is a production process manager for Node.js / io.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

So it has no promise for attaching executables or daemonized utilities. But attaching bash script file is a very handy thing on PM2. In my opinion if when stopped it closes bash process, mongo daemon will still live in this case you faced. So you may catch bash script exit situation and while stopping pm2 processes that caught exit situation you will be able to call mongod --shutdown command.

Or another solution create a start and stop scripts and run and stop daemonized utilities in these bash scripts like mongodb.

Extra solution is Docker?

Upvotes: 1

Related Questions