Node.js - 无法使用永远运行节点服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16715244/
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 14:35:26  来源:igfitidea点击:

Node.js - Unable to run node server using forever

node.jsforever

提问by S Singh

I am trying to run node server using forever command. I installed forever globally using:

我正在尝试使用永久命令运行节点服务器。我使用以下方法在全球永久安装:

npm install forever -g

After installing forever I try to run my node script by using below command:

永久安装后,我尝试使用以下命令运行我的节点脚本:

node_modules\.bin\forever start app.js

Below is my console:

下面是我的控制台:

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
info:    Forever processing file: app.js

Please help me to resolve this issue!

请帮我解决这个问题!

回答by Mike S.

There is no problem here other than warnings for configs forever recommends you declare. If you see the final message there it tells you it has processed your script. Just run forever listand you should see your script running.

除了永远建议您声明的配置警告之外,这里没有问题。如果您在那里看到最后的消息,它会告诉您它已经处理了您的脚本。只需运行forever list,您应该会看到您的脚本正在运行。

I ran into this same thing when installing npm via yum repository ( yum install npm) and then installing forever whereas when I install node and npm via shell scripts and then install forever it doesn't occur. It must have something to do with the formulas for the package installer or potentially missing alias with flags with installer to set those values behind the scene.

我在通过 yum 存储库 ( yum install npm)安装 npm然后永远安装时遇到了同样的事情,而当我通过 shell 脚本安装 node 和 npm 然后永远安装时,它不会发生。它必须与包安装程序的公式有关,或者可能缺少带有安装程序标志的别名以在幕后设置这些值。

Those don't mean it's not working. See below I created a js file using sample code from node's site and ran it manually (I flushed firewall to open port for app temporarily but you don't need that):

这些并不意味着它不起作用。见下文,我使用来自节点站点的示例代码创建了一个 js 文件并手动运行它(我刷新了防火墙以暂时为应用程序打开端口,但您不需要它):

[root@app1 ~]# vi example.js
[root@app1 ~]# apf -f
apf(23924): {glob} flushing & zeroing chain policies
apf(23924): {glob} firewall offline
[root@app1 ~]# node example.js 
Server running at http://127.0.0.1:1337/

I then start app using forever:

然后我开始使用永远的应用程序:

^C[root@app1 ~]# forever start example.js 
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: example.js

Now I check to see if my app is running:

现在我检查我的应用程序是否正在运行:

[root@app1 ~]# forever list
info:    Forever processes running
data:        uid  command       script     forever pid   logfile                 uptime       
data:    [0] dan1 /usr/bin/node example.js 23976   23978 /root/.forever/dan1.log 0:0:0:27.320 
[root@app1 ~]#

回答by Yonatan Ayalon

This solved my issue:

这解决了我的问题:

forever start -c node [path/to/app]

"-c" means - Run commnad; and then just run via nodejs

“-c”表示 - 运行命令;然后通过 nodejs 运行

This way - you get the Respawn by default of min. 1000ms uptime

这样 - 默认情况下您会获得 Respawn 分钟。1000 毫秒正常运行时间

Taken from: https://github.com/nodejitsu/forever/issues/422, by "Basarat"

摘自:https: //github.com/nodejitsu/forever/issues/422,由“Basarat”

回答by Sid Mhatre

If you are using node js with express framework then script will not start using :

如果您使用带有 express 框架的 node js,那么脚本将不会开始使用:

forever start app.js

First stop all running apps:

首先停止所有正在运行的应用程序:

forever stopall

When this Express framework used it must be started with:

当使用这个 Express 框架时,它必须以:

forever start ./bin/www

回答by saghar.fadaei

First stop all running apps:

首先停止所有正在运行的应用程序:

forever stopall

then use this command. It works for me and solved my issue:

然后使用这个命令。它对我有用并解决了我的问题:

forever -w ./bin/www

and you should find this in package.json file:

你应该在 package.json 文件中找到它:

"scripts": {
    "start": "node ./bin/www"
  }

I hope it helps you.

我希望它能帮助你。

回答by Rami

One thing that also produces same kind of output, but doesn't start the application is if foreveris unable to write to the specified log file. I had a case where the log file had become too big and this prevented the process from starting.

也产生相同类型的输出但不启动应用程序的一件事是,如果forever无法写入指定的日志文件。我遇到过日志文件变得太大的情况,这导致进程无法启动。

回答by Vahap Gencdal

Firstly change your package.json scripts like

首先更改您的 package.json 脚本,例如

"scripts": {
      "start": "forever ./bin/www.js"
 }

than start this command on linux console:sudo npm start

for windows just :npm start

比在 linux 控制台上启动此命令:sudo npm start

对于 Windows 只是 :npm start

回答by user1353936

I just ran into this today on an AWS Lightsail server, and NONE of the answers here or elsewhere had any effect. Everything worked fine until upgrading from NodeJS 10.x to 13.x. I tried removing and reinstalling forever, changing the permissions on the files and directories, etc, and I kept getting the EACCES error. The issue seemed to be that forever could not create directories within its .forever directory. The only thing that worked was to do the following:

我今天刚刚在 AWS Lightsail 服务器上遇到了这个问题,这里或其他地方的答案都没有任何影响。一切正常,直到从 NodeJS 10.x 升级到 13.x。我尝试永久删除并重新安装,更改文件和目录的权限等,但我一直收到 EACCES 错误。问题似乎是永远无法在其 .forever 目录中创建目录。唯一有效的是执行以下操作:

1) Remove the .forever folder and all its subfolders and contents. For me, this was accomplished as follows: sudo rm -rf /home/bitnami/.forever

1) 删除 .forever 文件夹及其所有子文件夹和内容。对我来说,这是按如下方式完成的:sudo rm -rf /home/bitnami/.forever

2) Manually recreate the .forever folder: sudo mkdir /home/bitnami/.forever

2) 手动重新创建 .forever 文件夹:sudo mkdir /home/bitnami/.forever

3) Manually set the permissions on the .forever folder: sudo chmod -R o+rwx /home/bitnami/.forever

3)手动设置.forever文件夹的权限:sudo chmod -R o+rwx /home/bitnami/.forever

4) Manually recreate the .forever/pids folder: sudo mkdir /home/bitnami/.forever/pids

4) 手动重新创建 .forever/pids 文件夹:sudo mkdir /home/bitnami/.forever/pids

5) Manually set the permissions on the .forever/pids folder: sudo chmod -R o+rwx /home/bitnami/.forever/pids

5) 手动设置 .forever/pids 文件夹的权限:sudo chmod -R o+rwx /home/bitnami/.forever/pids

6) Manually recreate the .forever/sock folder: sudo mkdir /home/bitnami/.forever/sock

6) 手动重新创建 .forever/sock 文件夹:sudo mkdir /home/bitnami/.forever/sock

7) Manually set the permissions on the .forever/sock folder: sudo chmod -R o+rwx /home/bitnami/.forever/sock

7) 手动设置 .forever/sock 文件夹的权限:sudo chmod -R o+rwx /home/bitnami/.forever/sock

8) Run my NodeJS app via forever again with the sudo command.

8) 使用 sudo 命令再次通过永远运行我的 NodeJS 应用程序。

9) List the processes forever is running, and verify that my app was there.

9) 列出永远正在运行的进程,并验证我的应用程序是否在那里。

I'm not sure why I had to go through all of this, as setting the permissions recursively should have done the same thing, but after doing this, forever started running perfectly as it did before.

我不确定为什么我必须经历所有这些,因为递归设置权限应该做同样的事情,但是在这样做之后,永远开始像以前一样完美运行。

Hope this helps someone.

希望这可以帮助某人。

回答by PierrickP

If you setup a module globaly (-g option) "forever" is in the $path

如果你设置了一个全局模块(-g 选项)“永远”在 $path 中

forever start app.js

should work.

应该管用。