简体   繁体   中英

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?

Because one is for websocket use and another one is for static web page use

Check out Forever on 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.

In Linux you can send each process to background by appending an & character giving you back the terminal control.

node server1.js &
node server2.js &

each one will return the process id in case you need to kill them.

If you want both servers to keep running after you exit the terminal session, you can use 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 . As of ubuntu release jaunty it comes pre packaged. I highly recommend learning about it, because it can be used for so much more than just 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).

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. For example, nodejs server1.js

  2. Type ctrl+a+d to exit the screen.

  3. Create a new screen by typing screen again.
  4. screen -ls to list screens and their individual ids.
  5. screen -x to re-attach to a recent screen.
  6. screen -r <id> to re-attach to a specific screen.
  7. kill -9 <id> to end a specific screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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