我希望我的节点应用程序连续运行.我确信在应用程序工作期间可能会出现一些崩溃.现在我可以看到3种让应用程序连续工作的方法:
问题是:这三种方式中哪一种更好用.在我看来,这个问题没有确切的答案,所以欢迎任何评论.
在我看来,第一个更合适,因为节点进程内存消耗不会随着时间而增长 - 每次节点进程崩溃永远都会启动另一个.
在尝试forever
在Ansible节点上启动进程时,我遇到了Anisble挂起的问题.我有一个非常简单的API服务器,我正在使用Ansible创建流浪和配置,如下所示:
---
- hosts: all
sudo: yes
roles:
- Stouts.nodejs
- Stouts.mongodb
tasks:
- name: Install Make Dependencies
apt: name={{ item }} state=present
with_items:
- gcc
- make
- build-essential
- name: Run NPM Update
shell: /usr/bin/npm update
- name: Create MongoDB Database Folder
shell: /bin/mkdir -p /data/db
notify:
- mongodb restart
- name: Generate Dummy Data
command: /usr/bin/node /vagrant/dataGen.js
- name: "Install forever (to run Node.js app)."
npm: name=forever global=yes state=latest
- name: "Check list of Node.js …
Run Code Online (Sandbox Code Playgroud) 我正在EC2实例上运行节点脚本.我正在使用forever.js来运行它,因为即使我断开SSH连接,我也想运行它.
我跑:
forever start app.js
Run Code Online (Sandbox Code Playgroud)
然后列出正在运行的进程:
forever list
Run Code Online (Sandbox Code Playgroud)
并且无论我做什么uptime
都说STOPPED
.
ubuntu@ip-xx-xxx-xxx-xxx:~$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 79f1 /usr/local/bin/node app.js 17099 17100 /home/ubuntu/.forever/79f1.log STOPPED
Run Code Online (Sandbox Code Playgroud) 除了永远传递脚本的路径(以及附带的选项,如上所述)之外,您还可以永远传递包含这些选项的JSON文件的路径.例如,考虑具有以下文件结构的应用程序:
在下面的例子中,选项uid
,append
,watch
,script
和sourceDir
设置.所有这些都是参数,以长版本的forever
命令(短版本是-a
,-w
,-s
).
我的问题是:一些选项,forever
没有一个长期的版本,例如-m
,-l
,-e
,-o
.如何在我的json配置文件中提供这些选项?
我已经尝试过将值添加到诸如"l"
和之类的键中"log"
,但是这并没有达到预期的效果.
When I use the killSignal flag when stopping a nodejs script with forever it ignores it. My command is (where index.js is the nodejs script):
forever --killSignal=SIGTERM stop index.js
Run Code Online (Sandbox Code Playgroud)
Am I using the flag the wrong way? Can anyone get the killSignal flag to work? I'm using forever version 0.15.1, installed globally.
我想创建一个像带有node的foreverjs一样运行的rabbitmq cli。它可以生成 child_process 并使其在后台运行,并且可以随时与 child_process 进行通信。我面临的问题是,当主 cli 程序退出时,child_process 似乎也停止运行,我尝试使用 detached:true 和 .unref() 进行分叉,但它不起作用。即使父调用者进程退出后,如何在后台运行子进程?
cli.js - 父级
const { fork, spawn } = require('child_process');
const options = {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
slient:true,
detached:true
};
child = fork('./rabbit.js', [], options)
child.on('message', message => {
console.log('message from child:', message);
child.send('Hi');
// exit parent
process.exit(0);
});
child.unref()
Run Code Online (Sandbox Code Playgroud)
rabbit.js - 如果子进程启动并运行,“i”应该继续递增
var i=0;
i++;
if (process.send) {
process.send("Hello"+i);
}
process.on('message', message => {
console.log('message from parent:', message);
});
Run Code Online (Sandbox Code Playgroud) 我今天刚刚将我的节点版本更新到 v16。现在,当我尝试使用永远来永远运行应用程序时,我收到一个错误,阻止我打开我的应用程序,我在谷歌上努力寻找解决方案,但我可以找到任何东西。有谁有解决办法或知道如何解决它?
(node:1498) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/fs/utils:879
throw new ERR_INVALID_ARG_TYPE(
^
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1505)
at Object.writeFileSync (node:fs:2146:5)
at writePid (/usr/lib/node_modules/forever/bin/monitor:13:6)
at exports.Monitor.<anonymous> (/usr/lib/node_modules/forever/bin/monitor:46:5)
at exports.Monitor.EventEmitter.emit (/usr/lib/node_modules/forever/node_modules/eventemitter2/lib/eventemitter2.js:339:22)
at /usr/lib/node_modules/forever/node_modules/forever-monitor/lib/forever-monitor/monitor.js:180:10
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
code: 'ERR_INVALID_ARG_TYPE'
}
Run Code Online (Sandbox Code Playgroud) 你如何设置环境变量来永远运行meteorjs?如果我进行导出并永远运行,就像这样
export PORT=80 MONGO_URL=mongodb://localhost:3002/appname
sudo forever -f start bundle/main.js
Run Code Online (Sandbox Code Playgroud)
永远不会运行带节点的meteorjs包:
/Users/../bundle/server/server.js:166
throw new Error("MONGO_URL must be set in environment");
^
Error: MONGO_URL must be set in environment
at run (/Users/../bundle/server/server.js:166:11)
at Object.<anonymous> (/Users/../bundle/server/server.js:331:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/../bundle/main.js:1:63)
at Module._compile (module.js:456:26)
error: Forever detected script exited with code: 8
Run Code Online (Sandbox Code Playgroud)
如果我直接运行它,它会顺利运行:
sudo PORT=80 MONGO_URL=mongodb://localhost:3002/appname ROOT_URL=http://myapp.com node bundle/main.js
Run Code Online (Sandbox Code Playgroud)
如果我这样做就像这个答案一样有效.但为什么它不适用于出口声明?有没有办法永远告诉配置文件来读取这些设置?我没有在永远的文档中看到一种方式.
在CentOS上使用NodeJS/Forever时,我的脚本会在接收到"SIGKILL"并重新启动的循环中被随机捕获.它每1-2秒执行一次,Forever日志显示如下内容:
2013-11-18T20:42:18.200Z - error: Forever detected script was killed by signal: SIGKILL
Run Code Online (Sandbox Code Playgroud)
这是一个服务器环境,没有人手动查杀脚本.我在CentOS配置中遗漏了一些可能在一定的正常运行时间之后杀死进程的东西吗?
有什么建议?
谢谢,-Nate
当在命令中运行节点通过特定端口时,我会以这种方式启动应用程序:
PORT=1234 node app.js
Run Code Online (Sandbox Code Playgroud)
如何将端口传递给forever
命令?无论我尝试什么,似乎都不想工作.
我试过了:
forever start app.js 1234
forever start app.js PORT=1234