简体   繁体   English

从 bat 文件启动节点和 pm2 应用程序

[英]start node and pm2 application from bat file

I have a chat-bot application running on node and I keep it always active thanks to pm 2.我有一个在 node 上运行的聊天机器人应用程序,由于 pm 2,我一直保持活动状态。

I would like to improve the way I launch the application.我想改进我启动应用程序的方式。 Instead of running the start command from the console, it would be nice to double click a .bat file.与其从控制台运行 start 命令,不如双击 .bat 文件。

I am trying to develop the bat file, but I lack knowledge.我正在尝试开发bat文件,但我缺乏知识。

I am grateful for any help.我很感激任何帮助。

@echo off
SET PM2_HOME=C:\Users\Usuario\.pm2
pm2 start C:\Users\Usuario\Desktop\ROBOTs\Chatbot SCTR\app.js
echo servicio ejecutado

This bat file that I developed does not work.我开发的这个bat文件不起作用。 I know that I am not calling the variables, because I don't know how to include it, since I always execute the pm2 start app.js command.我知道我没有调用变量,因为我不知道如何包含它,因为我总是执行 pm2 start app.js 命令。

my application does not use ports like 8080 and others, because the same library allows me to establish a connection and with pm 2 I keep it always active.我的应用程序不使用 8080 和其他端口,因为同一个库允许我建立连接,并且使用 pm 2 我保持它始终处于活动状态。

add the start command to your package.json for launching your app with pm2, then with your bat file just direct it to run with npm or yarn , whatever your default package manager is将 start 命令添加到您的package.json以使用 pm2 启动您的应用程序,然后使用您的 bat 文件直接将其与npmyarn一起npm ,无论您的默认包管理器是什么

edit:编辑:

here is a sample of a script in bash, but the concept will be the same for batch这是 bash 脚本的示例,但批处理的概念是相同的

#!/bin/bash

## detect operating system machine so we can setup some environment variables
UNAME="$(uname -s)"
case "${UNAME}" in
    Linux*)     OS='linux';;
    Darwin*)    OS='mac';;
    CYGWIN*)    OS='cygwin';;
    MINGW*)     OS='mingw';;
    *)          OS="UNKNOWN:${UNAME}"
esac

## if OS is Ubuntu (IE Production Box) set the path of variables
if [ $OS == 'linux' ]
then
  YARN=/usr/bin/yarn
  PM2=/usr/bin/pm2
fi
## if OS is Mac (IE Development Box) set the path of variables
if [ $OS == 'mac' ]
then
  YARN=/usr/local/bin/yarn
  PM2=/usr/local/bin/pm2
fi

## run the app
cd /var/www/application || exit
$YARN run productionStart

$PM2 save

exit $?

here is the line of code for starting the app on mac/linux we use from our package.json这是我们从 package.json 使用的用于在 mac/linux 上启动应用程序的代码行

"productionStart": "pm2 start ecosystem.config.js --env=production",

for more information about starting your app with an ecosystem file with pm2, see the docs here有关使用 pm2 使用生态系统文件启动应用程序的更多信息,请参阅 此处的文档

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

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