如何使 node.js 应用程序永久运行? [英] How to make a node.js application run permanently?

查看:34
本文介绍了如何使 node.js 应用程序永久运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Debian 服务器上,我安装了 Node.js.我了解如何使用以下命令行从腻子启动应用程序:

On a Debian server, I installed Node.js. I understand how to launch an app from putty with this command line:

node /srv/www/MyUserAccount/server/server.js

并通过地址 50.51.52.53:8080(IP 和端口)访问它.

and get to it on the address 50.51.52.53:8080 (IP and port).

但是一旦我关闭putty,我就无法再访问地址50.51.52.53:8080.

But as soon as I close putty, then I cannot reach the address 50.51.52.53:8080 anymore.

如何让 Node.js 应用永久运行?

How to make a Node.js application run permanently?

如您所料,我是 Linux 和 Node.js 的初学者.

As you can guess, I am a beginner with Linux and Node.js.

推荐答案

虽然其他答案解决了 OP 的问题,但它们都有些矫枉过正,并没有解释他或她为什么会遇到这个问题.

Although the other answers solve the OP's problem, they are all overkill and do not explain why he or she is experiencing this issue.

关键是这一行,我关闭putty,然后我无法到达地址"

The key is this line, "I close putty, then I cannot reach the address"

当您在 Putty 上登录远程主机时,您已经启动了一个 SSH linux 进程,并且从该 SSH 会话键入的所有命令都将作为该进程的子进程执行.

When you are logged into your remote host on Putty you have started an SSH linux process and all commands typed from that SSH session will be executed as children of said process.

您的问题是,当您关闭 Putty 时,您正在退出 SSH 会话,这会杀死该进程和任何活动的子进程.当您关闭 Putty 时,您会无意中杀死您的服务器,因为您在前台运行它.为了避免这种行为,通过附加 & 在 后台 中运行服务器.到您的命令:

Your problem is that when you close Putty you are exiting the SSH session which kills that process and any active child processes. When you close putty you inadvertently kill your server because you ran it in the foreground. To avoid this behavior run the server in the background by appending & to your command:

node /srv/www/MyUserAccount/server/server.js &

这里的问题是缺乏 linux 知识,而不是关于 node 的问题.有关更多信息,请查看:http://linuxconfig.org/understanding-foreground-and-background-linux-processes

The problem here is a lack of linux knowledge and not a question about node. For some more info check out: http://linuxconfig.org/understanding-foreground-and-background-linux-processes

更新:

正如其他人提到的,节点服务器在退出终端时仍然可能死亡.我遇到的一个常见问题是,即使节点进程在 bg 中运行,它的 stdout 和 stderr 仍然指向终端.这意味着如果节点服务器写入console.log 或console.error,它将收到管道损坏错误并崩溃.这可以通过管道输出过程来避免:

As others have mentioned, the node server may still die when exiting the terminal. A common gotcha I have come across is that even though the node process is running in bg, it's stdout and stderr is still pointed at the terminal. This means that if the node server writes to console.log or console.error it will receive a broken pipe error and crash. This can be avoided by piping the output of your process:

node /srv/www/MyUserAccount/server/server.js > stdout.txt 2> stderr.txt &

如果问题仍然存在,那么您应该查看诸如 tmuxnohup 之类的东西,它们仍然比特定于节点的解决方案更强大,因为它们可用于运行所有进程类型(数据库、日志服务、其他语言).

If the problem persists then you should look into things like tmux or nohup, which are still more robust than node specific solutions, because they can be used to run all types of processes (databases, logging services, other languages).

一个可能导致服务器退出的常见错误是,在运行 nohup 节点 your_path/server.js & 后,您只需单击一下即可关闭 Putty 终端.您应该改用 exit 命令,然后您的节点服务器将启动并运行.

A common mistake that could cause the server to exit is that after running the nohup node your_path/server.js & you simply close the Putty terminal by a simple click. You should use exit command instead, then your node server will be up and running.

这篇关于如何使 node.js 应用程序永久运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆