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
Keep alive express process after close the terminal
提问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
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 screen
or nohup
can work fine, but if it IS the case, it is best practice to use a daemon like pm2or forever.
我知道这是一个老问题,但是对于认同它的新访问者来说,我将在这里大胆猜测您想要运行一个一直运行的网络应用程序:一个可以不分昼夜地为访问者提供您的 IP 地址。如果不是这种情况,则screen
ornohup
可以正常工作,但如果是这种情况,最佳实践是使用像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.