如果服务器尚未运行,我想编写一个启动进程mongod的普通任务。我需要一个单神进程运行,但也需要咕噜-手表工作在稍后的任务流程。
This question解释了如何开始使用grunt-shell .接受的答案是阻塞,即使存在异步版本,异步版本也会生成一个新服务器。
是否有一种方法(例如,shell脚本)只在未运行的情况下启动mongod,而不阻塞其余的咕噜任务流?
谢谢
发布于 2013-12-20 20:38:36
,这是一个更干净的版本
将其作为startMongoIfNotRunning.sh存储在与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;在你的档案里:
shell: {
mongo: {
command: "sh startMongoIfNotRunning.sh",
options: {
async: true
}
},
}编辑-下面的原始版本
好吧-我觉得这很正常.
创建一个shell脚本,如果它不运行的话,它将启动mongod .把它保存在某个地方,可能在你的项目中。我把它命名为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您可能必须使其可执行:chmod +x path/to/script/startMongoIfNotRunning.sh
安装咕噜-外壳-产卵:npm install grunt-shell-spawn --save-dev
然后在Gruntfile中添加以下内容:
shell: {
mongo: {
command: "exec path/to/script/startMongoIfNotRunning.sh",
options: {
async: true
}
},
}(如果您使用的是yeoman,则使用<%= yeoman.app %>不能工作,因为这些路径相对于整个项目,所以您可以得到类似于'app‘之类的东西,而不是脚本的整个路径。我相信你能让它起作用,我只是不知道该怎么走)
如果您只是执行该任务,grunt shell:mongo mongod将启动,但我无法使用grunt shell:mongo:kill关闭它。但是,假设您稍后使用的是阻塞任务(我使用的是watch),那么当您结束该任务时,应该会自动关闭它。
希望这能帮到别人!
发布于 2013-12-21 13:38:45
我发现您的解决方案非常有用,但实际上是想在重新启动咕噜服务器时杀死mongod。所以我得到了这个
#!/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在我的mac电脑上效果很好。我的呼噜文件是这样的:
//used to load mongod via shell
shell: {
mongo: {
command: './mongo.sh',
options: {
async: true
}
}
}所以我的mongo.sh和我的Grunfile.js位于同一个位置
干杯
发布于 2015-05-14 09:24:39
另外两个答案是正确的。但是,为了完整起见,这里是Windows上等效的批处理脚本。将下列内容保存为startMongoIfNotRunning.bat
tasklist /fi "imagename eq mongod.exe" |find "=" > nul
if errorlevel 1 mongod如果有一个名为mongod.exe的任务正在运行,那么=字符应该出现在输出中--因此,如果它没有运行,将找不到=字符,错误级别变量将被设置为1。
其余的与@MaxBates的回答相同。
https://stackoverflow.com/questions/20696457
复制相似问题