简体   繁体   English

NodeJS是如何从ubuntu运行两个服务器文件?

[英]NodeJS is how can I run two server file from ubuntu?

Example like now if I run NodeJS on windows then I able to open 2 command prompt and run node server1.js and server2.js seperately but i am wondering how i can run this 2 server file on Linux ubuntu? 像现在这样的示例,如果我在Windows上运行NodeJS,则可以打开2命令提示符并分别运行节点server1.js和server2.js,但是我想知道如何在Linux ubuntu上运行此2服务器文件?

Because one is for websocket use and another one is for static web page use 因为一个供websocket使用,另一个供静态网页使用

Check out Forever on NPM. 永远在NPM上签出。

Once you install it you can just run 一旦安装,就可以运行

forever start app.js

and it will run in the background. 它将在后台运行。

You can also run 你也可以跑

forever list

to see all the processes that are running. 查看正在运行的所有进程。

Forever has the added benefit of restarting the processes if they crush. 如果进程崩溃,则Forever还会具有重新启动进程的额外好处。

In Linux you can send each process to background by appending an & character giving you back the terminal control. 在Linux中,您可以通过添加&字符将每个进程发送到后台,从而使您返回终端控件。

node server1.js &
node server2.js &

each one will return the process id in case you need to kill them. 如果您需要杀死每个进程,则每个进程都会返回该进程的ID。

If you want both servers to keep running after you exit the terminal session, you can use nohup: 如果您希望两个服务器在退出终端会话后都保持运行,则可以使用nohup:

nohup node server1.js &
nohup node server2.js &

How to create "a second command prompt" 如何创建“第二个命令提示符”

What you are looking for is called a terminal multiplexer . 您正在寻找的被称为终端多路复用器 There is a REALLY useful command called screen . 有一个非常有用的命令叫做screen As of ubuntu release jaunty it comes pre packaged. 作为Ubuntu的发布jaunty谈到预包装。 I highly recommend learning about it, because it can be used for so much more than just NodeJS. 我强烈建议您学习它,因为它不仅可以用于NodeJS,还可以用于更多用途。

Screen is a terminal multiplexer, which allows a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session (such as when using SSH). 屏幕是终端多路复用器,它允许用户访问单个终端窗口内的多个单独的终端会话或远程终端会话(例如使用SSH时)。

Use it like this 这样使用

  1. Type screen - This will create a "second window". 键入屏幕-这将创建一个“第二个窗口”。 Once you are in, you can type any commands you want, just like normal cli. 进入后,您可以键入所需的任何命令,就像普通cli一样。 For example, nodejs server1.js 例如, nodejs server1.js

  2. Type ctrl+a+d to exit the screen. 键入ctrl+a+d退出屏幕。

  3. Create a new screen by typing screen again. 通过再次键入screen创建一个新屏幕。
  4. screen -ls to list screens and their individual ids. screen -ls列出屏幕及其各个ID。
  5. screen -x to re-attach to a recent screen. screen -x重新附加到最近的屏幕。
  6. screen -r <id> to re-attach to a specific screen. screen -r <id>重新附加到特定屏幕。
  7. kill -9 <id> to end a specific screen. kill -9 <id>结束特定屏幕。

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

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