最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - grunt task: start mongod if not running - Stack Overflow

programmeradmin1浏览0评论

I want to write a grunt task to start the process mongod if the server is not already running. I need a mongod process running, but also need grunt-watch to work later in the task flow.

This question explains how to start mongod using grunt-shell ... the accepted answer is blocking, and the async version will spawn a new server even if one exists.

Is there a way (e.g. shell script) to start mongod only if it is not running, without blocking the rest of the grunt task flow?

Thanks

I want to write a grunt task to start the process mongod if the server is not already running. I need a mongod process running, but also need grunt-watch to work later in the task flow.

This question explains how to start mongod using grunt-shell ... the accepted answer is blocking, and the async version will spawn a new server even if one exists.

Is there a way (e.g. shell script) to start mongod only if it is not running, without blocking the rest of the grunt task flow?

Thanks

Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked Dec 20, 2013 at 4:45 Max BatesMax Bates 1,2781 gold badge16 silver badges21 bronze badges 3
  • stackoverflow./a/18275415/1085699 HAs a good solution for the script. – Nathaniel Johnson Commented Dec 20, 2013 at 5:31
  • im not really fluent with shell - can I just put all that on one line and it'll run? – Max Bates Commented Dec 20, 2013 at 5:33
  • No, you will have to make it a script and then call it from grunt. This might help: stackoverflow./questions/18368575/… – Nathaniel Johnson Commented Dec 20, 2013 at 5:36
Add a ment  | 

3 Answers 3

Reset to default 6

Here's a cleaner version

Store this as startMongoIfNotRunning.sh in same location as Gruntfile:

# this script checks if the mongod is running, starts it if not

if pgrep -q mongod; then
    echo running;
else
    mongod;
fi

exit 0;

And in your Gruntfile:

shell: {
    mongo: {
        mand: "sh startMongoIfNotRunning.sh",
        options: {
            async: true
        }
    },
}

Edit - original version below

Ok - I think this is working properly...

create a shell script which will start mongod if it's not running... save it somewhere, probably in your project. I named it startMongoIfNotRunning.sh :

# this script checks if the mongod is running, starts it if not

`ps -A | grep -q '[m]ongod'`

if [ "$?" -eq "0" ]; then
    echo "running"
else
    mongod
fi

You may have to make it executable: chmod +x path/to/script/startMongoIfNotRunning.sh

Install grunt-shell-spawn : npm install grunt-shell-spawn --save-dev

Then in your Gruntfile add this:

shell: {
      mongo: {
          mand: "exec path/to/script/startMongoIfNotRunning.sh",
          options: {
              async: true
          }
      },
}

(If you're using yeoman, using <%= yeoman.app %> didn't work because those paths are relative to the whole project, so you get something like 'app' instead of the whole path to the script. I'm sure you could get it working, I'm just not aware how to get the path to )

If you just execute the task grunt shell:mongo mongod will start but I wasn't able to close it using grunt shell:mongo:kill. However, assuming you're using a blocking task later (I'm using watch) then it should automatically be killed when you end that task.

Hope this helps someone!

I found your solution really helpful, but actually wanted to kill mongod when restarting grunt server. So I got this:

#!/bin/sh

# this script checks if the mongod is running, kills it and starts it

MNG_ID="`ps -ef | awk '/[m]ongod/{print $2}'`"

if [ -n "$MNG_ID" ]; then
    kill $MNG_ID
fi

mongod

which works really nice on my mac. And my grunt file looks like this:

//used to load mongod via shell
    shell: {
        mongo: {
            mand: './mongo.sh',
            options: {
                async: true
            } 
        }
    }

So my mongo.sh is in the same location as my Grunfile.js

Cheers

The other two answers are correct. However for pleteness here is the equivalent batch script on Windows. Save the following as startMongoIfNotRunning.bat:

tasklist /fi "imagename eq mongod.exe" |find "=" > nul
if errorlevel 1 mongod

If there is a task running called mongod.exe then the = character should appear in the output - hence if it is not running the = character will not be found and the errorlevel variable will be set to 1.

The rest is the same as @MaxBates answer.

发布评论

评论列表(0)

  1. 暂无评论