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

查看:150
本文介绍了如何使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).

但是一旦我关闭腻子,就无法到达地址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.

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

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会话,该会话会终止该进程和所有活动的子进程.关闭腻子时,由于在前景中运行服务器而无意中杀死了服务器.为避免这种情况,请通过添加&在后台中运行服务器.输入您的命令:

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知识,而不是有关节点的问题.有关更多信息,请查看: 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 &

如果问题仍然存在,那么您应该研究诸如 tmux nohup 之类的东西,它们比特定于节点的解决方案还要强大,因为它们可以用于运行所有解决方案.流程类型(数据库,日志记录服务,其他语言).

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 node 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天全站免登陆