bash 关闭终端后保持活动快递流程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26568135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:39:06  来源:igfitidea点击:

Keep alive express process after close the terminal

node.jsbashubuntuexpressamazon-ec2

提问by MrMins

I'm trying to keep live a process after close the terminal. Is a node.js project with express. Basically, for other process I kept alive processes with:

我试图在关闭终端后保持进程。是一个带有 express 的 node.js 项目。基本上,对于其他进程,我通过以下方式保持活动进程:

$ node server.js &

I with that, was possible finish the SSH connection and close the console. But with express, I started my process with:

有了这个,就可以完成 SSH 连接并关闭控制台。但是使用 express,我开始了我的过程:

$ npm start &

And always, after one request, the process died. Exist a way to keep the process alive? I'm using EC2, with a Ubuntu instance.

并且总是在一个请求之后,该进程就终止了。有没有办法让进程保持活力?我正在使用 EC2 和一个 Ubuntu 实例。

回答by MrMins

I found the answer https://unix.stackexchange.com/questions/89483/keeping-a-process-running-after-putty-or-terminal-has-been-closed

我找到了答案 https://unix.stackexchange.com/questions/89483/keeping-a-process-running-after-putty-or-terminal-has-been-closed

Basically, you can use nohupprocess. (Execute Commands After You Exit From a Shell Prompt).

基本上,您可以使用nohup进程。(从 Shell 提示退出后执行命令)。

And I used:

我用过:

$ nohup npm start &

And now is working good.

现在运行良好。

回答by JCollier

I know that this is an old question, but for new visitors who identify with it, I'm going to go out on a limb here and guess that you are wanting to run a web app that is up all of the time: one that can service visitors to your IP address, night and day. If that is not the case, then screenor nohupcan work fine, but if it IS the case, it is best practice to use a daemon like pm2or forever.

我知道这是一个老问题,但是对于认同它的新访问者来说,我将在这里大胆猜测您想要运行一个一直运行的网络应用程序:一个可以不分昼夜地为访问者提供您的 IP 地址。如果不是这种情况,则screenornohup可以正常工作,但如果是这种情况,最佳实践是使用像pm2永远这样的守护程序。

Daemons are arguably the best way to keep a service running all the time... like, how does your Ubuntu instance know when you want to talk to it via SSH? It's due to sshd, the SSH daemon. The daemon has code that is always listening, just like you would want a web-app to do.

守护进程可以说是保持服务始终运行的最佳方式……例如,您的 Ubuntu 实例如何知道您何时想通过 SSH 与之通信?这是由于 SSH 守护进程 sshd。守护进程具有始终在侦听的代码,就像您希望网络应用程序一样。

Also, pm2and foreverhave some other nice features, like booting up the daemon again if your computer happens to crash-and-reboot.

此外,pm2永远还有一些其他不错的功能,例如如果您的计算机碰巧崩溃并重新启动,则再次启动守护程序。

回答by Boris Detry

Use sceen

使用场景

on your terminal type :

在您的终端类型上:

screen

then

然后

npm start

And detach the session by pressing “Ctrl+a” immediately followed by “d”

并通过按“Ctrl+a”后紧跟“d”来分离会话

Here to attach the sesion again : Source

这里再次附上会议:来源

It works on CentOS7

它适用于 CentOS7