node.js Forever.js 启动和重启多个脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10065024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:28:34  来源:igfitidea点击:

Forever.js starting and restarting multiple scripts

node.jsdeployment

提问by Pickels

My web app has 3 main node.js components: website, feeds and jobs.

我的 Web 应用程序有 3 个主要的 node.js 组件:网站、提要和作业。

To start these I am using forever:

要开始这些我永远使用:

//forever.js

var forever = require('forever');

function start(name){

  forever.start( ['coffee', name + '.coffee'], { /* log options */ } )

};

start('website');
start('feeds');
start('jobs');

What I first noticed is that if I run script it wont run it as a daemon. ( Which is most likely normal )

我首先注意到的是,如果我运行脚本,它不会将它作为守护进程运行。(这很可能是正常的)

node forever.js

So what I did next was run the forever.js script with forever. I am not sure if this is correct, there is also a forever.startDaemon so not sure which one I should use.

所以我接下来做的是运行forever.js 脚本forever。我不确定这是否正确,还有一个 forever.startDaemon 所以不确定我应该使用哪个。

forever start forever.js

This works but the problem is that I would like to restart all the processes when a new version of my app is published. I am using git's post-receive hook to run the forever.js the first time but if I do this on each post-recieve it will just spawn 3 processes each time.

这有效,但问题是我想在发布新版本的应用程序时重新启动所有进程。我第一次使用 git 的 post-receive 钩子来运行forever.js,但是如果我在每个 post-receive 上都这样做,它每次只会产生 3 个进程。

So I guess I need a way to restart 3 processes if they are already running. I thought to do this with forever.list but the documentation only say:

所以我想我需要一种方法来重新启动 3 个进程,如果它们已经在运行。我想用forever.list来做这件事,但文档只说:

forever.list (format, callback)

Returns a list of metadata objects about each process that is being run using 
forever. This method is synchronous and will return the list of metadata as such.
Only processes which have invoked forever.startServer() will be available from
forever.list()

First of all I am not sure what format means and second it expects a callback but then it says its synchronous. Which is a little confusing and I am not sure how to use list.

首先,我不确定格式是什么意思,其次它需要一个回调,但它说它是同步的。这有点令人困惑,我不确定如何使用列表。

In the end all I want to do is start/restart 3 node.js processes on git's post-receive hook.

最后,我想要做的就是在 git 的 post-receive 钩子上启动/重启 3 个 node.js 进程。

回答by FGRibreau

I think the best way to do this is:

我认为最好的方法是:

forever start website.js
forever start feeds.js
forever start jobs.js

and then in your git post-receive hook:

然后在你的 git post-receive 钩子中:

forever restart website.js
forever restart feeds.js
forever restart jobs.js

Wrapping these node processes inside a single process is not a good idea. I now personally use Supervisord with monitinstead of forever (supervisord is more stable & powerful than forever IMHO).

将这些节点进程包装在单个进程中并不是一个好主意。我现在个人将Supervisord 与 monit 一起使用而不是永远(supervisord 比永远恕我直言更稳定和强大)。

回答by flatroze

I do it like this:

我这样做:

#!/bin/sh

# Make sure we're in the right place
DIR=$(cd $(dirname "
"scripts": {
    "forever" : "forever start api/api-server.js && forever start www/www-server.js && forever start upload/upload-server.js && forever start static/static-server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
}
"); pwd) cd $DIR echo "[ I am $USER and I changed PWD to $DIR ]" forever restart --spinSleepTime=2000 api_daemon.js || (forever start --spinSleepTime=2000 api_daemon.js && forever list)

Works like a charm, I never get duplicate processes using ./run.sh

像魅力一样工作,我从来没有使用 ./run.sh 获得重复的进程

To read logs, I use tail -f /path/to/.log

要读取日志,我使用 tail -f /path/to/.log

回答by Uday Hiwarale

Yes, it's possible. You need to use npm run forevercommand to run a script.

是的,这是可能的。您需要使用npm run forever命令来运行脚本。

Add this to your package.json

将此添加到您的 package.json

##代码##

You can create package.json using npm init

您可以使用创建 package.json npm init