简体   繁体   English

使用ssh在linux服务器上启动nodejs应用-如果我关闭ssh连接,应用停止了吗?)

[英]Start nodejs app on linux server with ssh-if i close the ssh connection,app stopped why?)

用ssh在linux服务器上启动nodejs应用程序(如果我关闭ssh连接,应用程序停止了吗?)1-创建nodejs应用程序-它的功能2-在Linux服务器上运行-它的功能(我停止了Apache服务器)但是如果我关闭了SSH连接(与我的Windows PC),应用程序停止。我该如何解决此问题?

Use the nohup command to start the application. 使用nohup命令启动应用程序。 Like: 喜欢:

nohup THE_COMMAND_YOU_DONT_WANT_TO_STOP_WHEN_YOU_LOGOUT &

With nodemon it might be helpful to put the command to start the server in a file called myserver.sh containing: 使用nodemon时,将命令启动服务器放在名为myserver.sh的文件中可能会有所帮助,该文件包含:

nodemon server.js

Make sure the file is executable: 确保文件是可执行文件:

chmod +x myserver.js

And then run 然后跑

nohup myserver.sh &

The most correct thing to do is to write a service file for it so whatever init system you have (likely systemd) will keep it running and manage the start/stop/restart stuff for you. 最正确的做法是为其编写一个服务文件,以便无论您拥有什么初始化系统(可能是systemd),它都可以保持运行,并为您管理启动/停止/重新启动的工作。

Failing that (and I don't blame you...) you can run it within the screen utility. 如果没有(我不怪你...),您可以在screen实用程序中运行它。 Launch it with screen -d -m /path/to/start/script and then you can come back later and reconnect to it with screen -r or screen -r <pid of the screen session> . 使用screen -d -m /path/to/start/script启动它,然后稍后可以使用screen -rscreen -r <pid of the screen session>重新连接到它。

Note that launching it that way won't restart it, etc. To do that, you could do something like 请注意,以这种方式启动它不会重启它,依此类推。您可以执行类似的操作

#!/bin/sh

while true
do
 sleep 3s
 /path/to/start/script
done

And call that with the screen command. 并使用screen命令调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM