简体   繁体   中英

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.

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.

I am trying to develop the bat file, but I lack knowledge.

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. 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.

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.

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

edit:

here is a sample of a script in bash, but the concept will be the same for batch

#!/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

"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

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