简体   繁体   English

使用shell脚本从单个shell命令运行两个节点服务器

[英]Running two node servers from a single shell command using a shell script

I have to run two node servers in different port, I want to write a simple shell script that will start both of the servers. 我必须在不同的端口运行两个节点服务器,我想编写一个简单的shell脚本来启动这两个服务器。

I wrote it like below: 我写的如下:

node project/rest.js && node static-server.js

but when I run the commands at a time, it starts the first server and dont execute the second one. 但是当我一次运行命令时,它会启动第一个服务器而不执行第二个服务器。

And only the fist server listens for request, second static server doesn't start. 并且只有第一个服务器侦听请求,第二个静态服务器不启动。 And in the shell I do have a output from rest.js. 在shell中我确实有rest.js的输出。

What I previously did to run tow servers, I run two commands in different shell. 我以前做过什么来运行两个服务器,我在不同的shell中运行两个命令。

Is there a way I can run both of the server with a single shell script? 有没有办法可以使用单个shell脚本运行两个服务器?

Thanks in advance. 提前致谢。

Your command does not work because you are trying to have two processes running in the same shell. 您的命令不起作用,因为您尝试在同一个shell中运行两个进程。 Instead, you should 'spawn' the node processes into different processes. 相反,您应该将节点进程“生成”到不同的进程中。 Try this command: 试试这个命令:

node project/rest.js & node static-server.js &

i have written very simple shell script, for starting MongoDB, start multiple node servers in a new terminal window and open Webstorm Ide. 我编写了非常简单的shell脚本,用于启动MongoDB,在新的终端窗口中启动多个节点服务器并打开Webstorm Ide。

it uses https://github.com/mklement0/ttab library, for opening new terminal windows. 它使用https://github.com/mklement0/ttab库来打开新的终端窗口。

you can specify multiple projects in code and with simple if else or maybe with switch statement, differentiate projects. 您可以在代码中指定多个项目,并使用简单的if else或者使用switch语句来区分项目。

sh dev.sh waufwauf   

will cd and start waufwauf project, hope it helps. 将cd并启动waufwauf项目,希望它有所帮助。

#!/bin/bash

if [ "$1" = "waufwauf" ]
then
  cd Code/waufwauf;
    ttab -d ./ mongod;
    ttab -d server npm run dev;
    ttab -d client npm run dev;
    wstorm ~/Code/waufwauf;
elif [ "$1" = "" ]
then
    echo specify project name;  
else
    echo $1 unknown project;
fi

try this, this will work, 试试这个,这会有效,

start node project/rest.js && start node static-server.js start node project / rest.js && start node static-server.js

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

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