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.
我通过ssh连接到远程服务器。然后我开始我的结点。js应用永远。在我关闭控制台窗口之前,一切正常。如何管理节点。我的远程服务器上永远都有js应用,即使我通过ssh关闭连接?我只想启动一个应用程序,关闭我的电脑。我的应用程序应该在我的远程服务器的后台运行。
9 个解决方案
#1
52
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.
您可能还想考虑使用upstart实用程序。它将允许您像服务一样启动、停止和重新启动节点应用程序。Upstart可以配置为在应用程序崩溃时自动重启它。
Install upstart:
安装的:
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
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:
然后将脚本文件(myapp.conf)复制到/etc/init,并确保其标记为可执行文件。然后可以使用以下命令来管理应用程序:
sudo start myapp
sudo stop myapp
sudo restart myapp
#2
24
Two answers: One for Windows, one for *nix:
两个答案:一个是Windows,一个是*nix:
On Windows, you can use the start
command to start the process disconnected from your instance of cmd.exe
:
在Windows上,您可以使用start命令启动从您的实例cmd.exe断开连接的进程:
start node example.js
On *nix, there are two aspects of this: Disconnecting the process from the console, and making sure it doesn't receive the HUP
signal ("hang up"), which most processes (including Node) will respond to by terminating. The former is possibly optional, but the latter is necessary.
在*nix上,有两个方面:从控制台断开进程,并确保它不接收HUP信号(“挂起”),大多数进程(包括节点)将通过终止来响应这个信号。前者可能是可选的,但后者是必要的。
Starting disconnected from the console is easy: Usually, you just put an ampersand (&
) at the end of the command line:
从控制台断开连接很容易:通常,您只需在命令行末尾添加一个& (&):
# Keep reading, don't just grab this and use it
node example.js &
But the above doesn't protect the process from HUP
signals. The program may or may not receive HUP
when you close the shell (console), depending on a shell option called huponexit
. If huponexit
is true
, the process will receive HUP
when the shell exits and will presumably terminate.
但是上面并没有保护进程不受HUP信号的影响。当您关闭shell(控制台)时,程序可能接收HUP,也可能不接收,这取决于称为huponexit的shell选项。如果huponexit为真,则该进程将在shell退出时接收HUP并可能终止。
huponexit
defaults to false
on the various Linux variants I've used, and in fact I happily used the above for years until coderjoe and others helped me understand (in a very long comment stream under the answer that may have since been deleted) that I was relying on huponexit
being false
.
huponexit默认为false的各种Linux变体我使用,事实上,我愉快地使用上述多年直到coderjoe和其他人帮助我理解(在一个很长的评论流下答案,可能已经被删除),我是依靠huponexit是假的。
To avoid the possibility that huponexit
might be true
in your environment, explicitly use nohup
. nohup
runs the process immune from HUP
signals. You use it like this:
为了避免huponexit在您的环境中可能是正确的,可以显式地使用nohup。nohup运行的过程不受HUP信号的影响。你可以这样使用它:
nohup node example.js > /dev/null &
or
或
nohup node example.js > your-desired-filename-or-stream-here &
The redirection is important; if you don't do it, you'll end up with a nohup.out
file containing the output from stdout
and stderr
. (By default, nohup
redirects stderr
to stdout
, and if stdout
is outputting to a terminal, it redirects that to nohup.out
. nohup
also redirects stdin
if it's receiving from a terminal, so we don't have to do that. See man nohup
or info coreutils 'nohup invocation'
for details.)
重定向是非常重要的;如果你不这样做,最后你会被打得鼻青脸肿。包含stdout和stderr输出的out文件。(默认情况下,nohup将stderr重定向到stdout,如果stdout输出到终端,它会将其重定向到nohup.out。如果stdin从终端接收,nohup也会重定向,所以我们不需要这么做。详见man nohup或info coreutils 'nohup调用。
In general for these things, you want to use a process monitor so that if the process crashes for some reason, the monitor restarts it, but the above does work for simple cases.
一般来说,对于这些事情,您希望使用一个进程监视器,这样当进程由于某种原因而崩溃时,监视器会重新启动它,但是上面的操作只适用于简单的情况。
#3
17
Always, simple is the best, no need upstart, no need forever, just nohup:
永远,简单是最好的,不需要暴发户,不需要永远,只要nohup:
nohup node file.js &
nohup节点文件。js &
Believe me, I'm running so that for my case!
相信我,我这么做是为了我的案子!
#4
3
node expamle.js &
for example
节点expamle。js &例如
#5
3
In Linux, SSH into your remote server and run
在Linux中,SSH到您的远程服务器并运行。
screen
to launch into a new screen.
进入一个新的屏幕。
Finally, type ctrlad to detach the screen session without killing the process.
最后,键入ctrl +分隔屏幕会话,不杀死进程。
More info here.
更多的信息在这里。
#6
3
I would definitely recommend pm2
npm install -g pm2
我绝对推荐pm2 npm安装-g pm2
To start server: pm2 start [yourServerFile.js]
To stop server: pm2 stop [yourServerFile.js]
要启动服务器:pm2启动[您的服务器文件。要停止服务器:pm2停止[yourServerFile.js]
Close client and server will run forever....will also restart if app crashes.
Ive been running a node server on Ubuntu for months with zero issues
关闭客户端和服务器将运行永远....如果应用程序崩溃,也将重新启动。我已经在Ubuntu上运行了几个月的节点服务器,没有问题。
#7
1
This is only a partial answer for Windows. I’ve created a single line Visual Basic Script called app.vbs
that will start your node application within a hidden window:
这只是Windows的部分答案。我已经创建了一个名为app.vbs的单行Visual Basic脚本,它将在一个隐藏的窗口中启动您的节点应用程序:
CreateObject("Wscript.Shell").Run "node app.js", 0
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\
directory and add a shortcut to the app.vbs
file.
要在启动时自动执行,请打开%AppData%\Microsoft\Windows\Start菜单程序\ startup \ directory并在app.vbs文件中添加快捷方式。
More info at: https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
更多信息:https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
#8
1
I had similar issue and I think using forever will help to handle crashed and restarts
我也遇到过类似的问题,我认为永久使用将有助于处理崩溃和重新启动
You can install forever globally: sudo nom install -g forever
您可以在全球永远安装:sudo nom永远安装-g
And run this command:
运行这个命令:
nohup forever server.js &
This should handle all the trouble of closing the terminal, closing ssh session, node crashes and restarts.
这将处理关闭终端、关闭ssh会话、节点崩溃和重新启动的所有麻烦。
#9
1
If you're running node.js in a production environment, you should consider using PM2, forever.js, or Nodemon.
如果你运行节点。在生产环境中,应该考虑永远使用PM2。js或Nodemon。
There is no shortage of articles online comparing the different packages.
网上不乏比较不同包装的文章。
#1
52
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.
您可能还想考虑使用upstart实用程序。它将允许您像服务一样启动、停止和重新启动节点应用程序。Upstart可以配置为在应用程序崩溃时自动重启它。
Install upstart:
安装的:
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
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:
然后将脚本文件(myapp.conf)复制到/etc/init,并确保其标记为可执行文件。然后可以使用以下命令来管理应用程序:
sudo start myapp
sudo stop myapp
sudo restart myapp
#2
24
Two answers: One for Windows, one for *nix:
两个答案:一个是Windows,一个是*nix:
On Windows, you can use the start
command to start the process disconnected from your instance of cmd.exe
:
在Windows上,您可以使用start命令启动从您的实例cmd.exe断开连接的进程:
start node example.js
On *nix, there are two aspects of this: Disconnecting the process from the console, and making sure it doesn't receive the HUP
signal ("hang up"), which most processes (including Node) will respond to by terminating. The former is possibly optional, but the latter is necessary.
在*nix上,有两个方面:从控制台断开进程,并确保它不接收HUP信号(“挂起”),大多数进程(包括节点)将通过终止来响应这个信号。前者可能是可选的,但后者是必要的。
Starting disconnected from the console is easy: Usually, you just put an ampersand (&
) at the end of the command line:
从控制台断开连接很容易:通常,您只需在命令行末尾添加一个& (&):
# Keep reading, don't just grab this and use it
node example.js &
But the above doesn't protect the process from HUP
signals. The program may or may not receive HUP
when you close the shell (console), depending on a shell option called huponexit
. If huponexit
is true
, the process will receive HUP
when the shell exits and will presumably terminate.
但是上面并没有保护进程不受HUP信号的影响。当您关闭shell(控制台)时,程序可能接收HUP,也可能不接收,这取决于称为huponexit的shell选项。如果huponexit为真,则该进程将在shell退出时接收HUP并可能终止。
huponexit
defaults to false
on the various Linux variants I've used, and in fact I happily used the above for years until coderjoe and others helped me understand (in a very long comment stream under the answer that may have since been deleted) that I was relying on huponexit
being false
.
huponexit默认为false的各种Linux变体我使用,事实上,我愉快地使用上述多年直到coderjoe和其他人帮助我理解(在一个很长的评论流下答案,可能已经被删除),我是依靠huponexit是假的。
To avoid the possibility that huponexit
might be true
in your environment, explicitly use nohup
. nohup
runs the process immune from HUP
signals. You use it like this:
为了避免huponexit在您的环境中可能是正确的,可以显式地使用nohup。nohup运行的过程不受HUP信号的影响。你可以这样使用它:
nohup node example.js > /dev/null &
or
或
nohup node example.js > your-desired-filename-or-stream-here &
The redirection is important; if you don't do it, you'll end up with a nohup.out
file containing the output from stdout
and stderr
. (By default, nohup
redirects stderr
to stdout
, and if stdout
is outputting to a terminal, it redirects that to nohup.out
. nohup
also redirects stdin
if it's receiving from a terminal, so we don't have to do that. See man nohup
or info coreutils 'nohup invocation'
for details.)
重定向是非常重要的;如果你不这样做,最后你会被打得鼻青脸肿。包含stdout和stderr输出的out文件。(默认情况下,nohup将stderr重定向到stdout,如果stdout输出到终端,它会将其重定向到nohup.out。如果stdin从终端接收,nohup也会重定向,所以我们不需要这么做。详见man nohup或info coreutils 'nohup调用。
In general for these things, you want to use a process monitor so that if the process crashes for some reason, the monitor restarts it, but the above does work for simple cases.
一般来说,对于这些事情,您希望使用一个进程监视器,这样当进程由于某种原因而崩溃时,监视器会重新启动它,但是上面的操作只适用于简单的情况。
#3
17
Always, simple is the best, no need upstart, no need forever, just nohup:
永远,简单是最好的,不需要暴发户,不需要永远,只要nohup:
nohup node file.js &
nohup节点文件。js &
Believe me, I'm running so that for my case!
相信我,我这么做是为了我的案子!
#4
3
node expamle.js &
for example
节点expamle。js &例如
#5
3
In Linux, SSH into your remote server and run
在Linux中,SSH到您的远程服务器并运行。
screen
to launch into a new screen.
进入一个新的屏幕。
Finally, type ctrlad to detach the screen session without killing the process.
最后,键入ctrl +分隔屏幕会话,不杀死进程。
More info here.
更多的信息在这里。
#6
3
I would definitely recommend pm2
npm install -g pm2
我绝对推荐pm2 npm安装-g pm2
To start server: pm2 start [yourServerFile.js]
To stop server: pm2 stop [yourServerFile.js]
要启动服务器:pm2启动[您的服务器文件。要停止服务器:pm2停止[yourServerFile.js]
Close client and server will run forever....will also restart if app crashes.
Ive been running a node server on Ubuntu for months with zero issues
关闭客户端和服务器将运行永远....如果应用程序崩溃,也将重新启动。我已经在Ubuntu上运行了几个月的节点服务器,没有问题。
#7
1
This is only a partial answer for Windows. I’ve created a single line Visual Basic Script called app.vbs
that will start your node application within a hidden window:
这只是Windows的部分答案。我已经创建了一个名为app.vbs的单行Visual Basic脚本,它将在一个隐藏的窗口中启动您的节点应用程序:
CreateObject("Wscript.Shell").Run "node app.js", 0
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\
directory and add a shortcut to the app.vbs
file.
要在启动时自动执行,请打开%AppData%\Microsoft\Windows\Start菜单程序\ startup \ directory并在app.vbs文件中添加快捷方式。
More info at: https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
更多信息:https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
#8
1
I had similar issue and I think using forever will help to handle crashed and restarts
我也遇到过类似的问题,我认为永久使用将有助于处理崩溃和重新启动
You can install forever globally: sudo nom install -g forever
您可以在全球永远安装:sudo nom永远安装-g
And run this command:
运行这个命令:
nohup forever server.js &
This should handle all the trouble of closing the terminal, closing ssh session, node crashes and restarts.
这将处理关闭终端、关闭ssh会话、节点崩溃和重新启动的所有麻烦。
#9
1
If you're running node.js in a production environment, you should consider using PM2, forever.js, or Nodemon.
如果你运行节点。在生产环境中,应该考虑永远使用PM2。js或Nodemon。
There is no shortage of articles online comparing the different packages.
网上不乏比较不同包装的文章。