代码之家  ›  专栏  ›  技术社区  ›  Xantium

使用批处理文件同时运行多个NodeJS项目

  •  1
  • Xantium  · 技术社区  · 6 年前

    我有这样的文件结构:

    bots -|
          |- test1 -|
                    |- index.js
                    |- activate.bat
                    |- token.txt
    
          |- test2 -|
                    |- index.js
                    |- actiate.js
                    |- token.txt
    
          |- run_all.bat
    

    index.js 要运行的文件是否是 node 命令。两者兼而有之 索引.js token.txt 文件。这个 索引.js 文件读取文本文件

    activate.bat 就是这样:

    @echo off
    node index.js
    

    .bat 档案,这是哪里 run_all.bat 进来。

    激活.bat 所以:

    @echo off
    
    echo Starting all bots...
    
    start /b node test1\index.js
    start /b node test2\index.js
    

    但是我得到一个错误:

    PS C:\Users\Simon\Desktop\bot> ./run_all.bat
    Starting all bots...
    PS C:\Users\Simon\Desktop\bot> test1 running
    test2 running
    (node:10176) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.
        at Promise (C:\Users\Simon\node_modules\discord.js\src\client\rest\RESTMethods.js:34:54)
        at new Promise (<anonymous>)
        at RESTMethods.login (C:\Users\Simon\node_modules\discord.js\src\client\rest\RESTMethods.js:33:12)
        at Client.login (C:\Users\Simon\node_modules\discord.js\src\client\Client.js:279:30)
        at ReadFileContext.callback (C:\Users\Simon\Desktop\bot\test1\index1.1.js:88:12)
        at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:235:13)
    (node:4836) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.
        at Promise (C:\Users\Simon\node_modules\discord.js\src\client\rest\RESTMethods.js:34:54)
        at new Promise (<anonymous>)
        at RESTMethods.login (C:\Users\Simon\node_modules\discord.js\src\client\rest\RESTMethods.js:33:12)
        at Client.login (C:\Users\Simon\node_modules\discord.js\src\client\Client.js:279:30)
        at ReadFileContext.callback (C:\Users\Simon\Desktop\bot\test2\index.js:65:12)
        at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:235:13)
    (node:10176) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
    (node:4836) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
    (node:10176) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    (node:4836) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    

    每次运行时 激活.bat 独自一人 不要 得到这个错误,出什么问题了?我的批处理命令是不是把不同的 令牌.txt

    1 回复  |  直到 6 年前
        1
  •  2
  •   Mofi    4 年前

    主要问题可以用期权来解决 /D 指挥权 开始 index.js 很明显文件 token.txt 在当前目录中,并且当前目录是包含这两个文件的目录。在双击批处理文件时,Windows默认将当前目录设置为批处理文件的目录 activate.bat .

    @echo off
    echo Starting all bots...
    start "" /B /D"%~dp0test1" node index.js
    start "" /B /D"%~dp0test2" node index.js
    

    开始 node 开始 . 在本例中,使用指定的空窗口标题 "" 这是一个很好的选择,也是启动一个WindowsGUI应用程序的好选择,因为根本没有打开控制台窗口。

    子目录 test1 test2 总是在包含批处理文件的目录中 run_all.bat . 因为这个原因 %~dp0 用于引用参数0的驱动器和路径,参数0是当前执行的批处理文件。这使批处理文件 跑_全部.bat 它本身独立于执行时的当前目录是什么 .

    / (正斜杠)是Linux/Mac上的目录分隔符。但是 \ (反斜杠)应在Windows上的文件/文件夹路径中使用,因为 / 用于Windows上的选项,但从Unix移植到Windows的应用程序不需要适应Windows语法。用于文件/文件夹访问的Windows内核函数替换所有 / \

    %~dp0秒 扩展为始终以反斜杠结尾的路径字符串。因此,在将这个动态字符串与固定的文件/文件夹名/路径连接时,不应使用额外的反斜杠。否则会有两个 \ 一系列的执行。文件/文件夹访问的Windows内核函数也会自动更正 \\ \ . 但最好还是避免这个错误。

    Naming Files, Paths, and Namespaces

    所以命令 索引.js 令牌.txt 节点 索引.js

    更好的解决方案是使用此批处理文件:

    @echo off
    echo Starting all bots...
    for /F "delims=" %%I in ('dir "%~dp0index.js" /A-D-H /B /S 2^>nul') do start "" /B /D"%%~dpI" node index.js
    

    命令 在单独的命令进程中运行 cmd.exe /C (更准确地说 %CompSpec% /C

    dir "C:\Users\Simon\Desktop\bot\index.js" /A-D-H /B /S 2>nul
    

    方向 要处理的输出 已启动的命令进程

    • 由于选项的原因,以裸格式显示 /B
    • 带扩展名的文件名 由于选项而具有完整路径 /S
    • /A-D-H (属性不是目录也不是隐藏的)
    • 索引.js 由于选项,在指定目录或任何子目录中找到 /S

    由输出的错误消息 方向 处理 如果找不到符合这些条件的任何内容,则重定向到设备 .

    阅读有关的Microsoft文档 Using Command Redirection Operators 为了解释 2>nul . 重定向操作符 > 必须用插入符号字符转义 ^ 它执行嵌入的 dir

    有选择权 /F 捕获要处理的输出 标准输出 已启动后台命令进程,并在启动后处理此输出 cmd.exe ; 默认情况下也被忽略 因为分号是默认的行尾选项。不可能通过 方向 eol=; 在这种情况下可以保存。 delims= 它定义了一个空的分隔符列表,结果总是获得分配给循环变量的完整限定文件名。

    对输出的每个文件名执行 方向 命令 如上所述。这无关紧要 如果使用指定的目录路径 /D"..." 结尾没有或有反斜杠。

    与第一个批处理文件解决方案相比,第二个批处理文件解决方案的主要优势显而易见: 索引.js .

    节点 实例并行可能会适得其反,这取决于 索引.js 停止批处理文件的执行直到 节点 在达到有效的最大并行运行数时,实例在启动下一个实例之前终止了自身 节点 实例。

    要了解所使用的命令及其工作方式,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页。

    • dir /?
    • echo /?
    • for /?
    • start /?

    节点 至少有文件扩展名,最好有完整路径,使批处理文件独立于环境变量 PATHEXT PATH