问题描述
我通过 ssh 连接到我的远程服务器.然后我用 Forever 启动我的 node.js 应用程序.一切正常,直到我关闭控制台窗口.即使我通过 ssh 关闭连接,如何在我的远程服务器上永远运行 node.js 应用程序?我只想启动一个应用程序并关闭我的电脑.我的应用应该在我的远程服务器的后台运行.
I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my remote server even when I close my connection via ssh? I just want to start an app and shut down my copmputer. My app should be working in the background on my remote server.
推荐答案
您可能还需要考虑使用 upstart 实用程序.它将允许您像服务一样启动、停止和重新启动节点应用程序.Upstart 可以配置为在应用程序崩溃时自动重启.
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
安装新贵:
sudo apt-get install upstart
为您的应用程序创建一个简单的脚本,如下所示:
Create a simple script for your application that will look something like:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1
然后将脚本文件 (myapp.conf) 复制到/etc/init 并确保其标记为可执行文件.然后可以使用以下命令管理您的应用程序:
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
sudo start myapp
sudo stop myapp
sudo restart myapp
这篇关于控制台关闭时如何永远运行 node.js 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!