Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

https://www.npmjs.com/package/pm2

https://www.npmjs.com/package/pm2-windows-service

https://blog.cloudboost.io/nodejs-pm2-startup-on-windows-db0906328d75

https://pm2.keymetrics.io/docs/usage/quick-start/


Overview

PM2 allows you to process applications in the background. It provides several benefits, for example logs management, automatic restart policies, application monitors to name a few.

Using PM2, we can:
1. start/stop/list components (pm2 start apps/app1.js)
2. load a predefined configuration (pm2 reload ecosystem.json.js)
3. save current configuration (pm2 save)
4. restore previously saved configuration (pm2 resurrect)


Installation

Installing PM2

npm install pm2 -g


Start an application

From within your profoundjs folder type:

pm2 start start.js

This will create a default PM2 home folder (under C:\Users\<username>\.pm2) that will store PM2 relevant files


Change PM2 Home Folder Location

Now, we will move that folder to a brand new folder: c:\pm2home\.pm2


Follow these steps:
1. Create a new folder c:\pm2home\ 
2. Copy the .pm2 folder from C:\Users\<username>\ to c:\pm2home\
3. Create a new PM2_HOME variable (at System level, not User level) and set the value c:\pm2home\.pm2   ( See Images Below ) 
4. Close all your open terminal windows (or restart Windows)
5. Ensure that your PM2_HOME has been set properly, running echo %PM2_HOME%   ( Windows Terminal: echo $Env:PM2_HOME ) 


           



Creating a Configuration File

When managing multiple applications with PM2, use a JS configuration file to organize them.

To generate a configuration file:

From the pm2home\ directory that you created above, 
Type:  
pm2 init simple 

This will create a file called:  ecosystem.config.js that will be your configuration file.
From here you can give the start.js a meaning name, for example: Production, QA, Development and include the location of the javascript.




“exec_mode” defines application running mode. It can have two types either “fork” or “cluster” mode. Fork mode is helpful when utilizing pm2 for PHP and python. Cluster mode is defined for node applications which will eventually access NodeJs cluster module on a lower level. It is great for zero-configuration based process management.

Launch Configuration Script

From the PM2Home directory type:

pm2 start ecosystem.config.js

This will launch all of the environments that you have setup in the config file.


Saving configuration

To save your configuration type:

pm2 save


Create Windows Service

Using pm2-windows-service

We will create a Windows Service using Node module pm2-windows-service, check here for more info.
This will allow the PM2 instances to auto run.


1. Install Node module
npm install -g pm2-windows-service

2. As administrator, open command line, run:
pm2-service-install -n PM2
and set the following:
? Perform environment setup (recommended)? Yes
? Set PM2_HOME? Yes
? PM2_HOME value (this path should be accessible to the service user and
should not contain any “user-context” variables [e.g. %APPDATA%]): c:\pm2home\.pm2\
? Set PM2_SERVICE_SCRIPTS (the list of start-up scripts for pm2)? No
? Set PM2_SERVICE_PM2_DIR (the location of the global pm2 to use with the service)? [recommended] Yes
? Specify the directory containing the pm2 version to be used by the
service C:\USERS\<USER>\APPDATA\ROAMING\NPM\node_modules\pm2\index.js
PM2 service installed and started.

This will create a Windows Service called PM2


Testing

1. Restart Windows
2. Just after restart, open command prompt (as Administrator) and run
pm2 status→ our application is running ;)


Conclusion

At this point, you would have your Node Programs Setup as a Windows Service program which will auto load upon restart.



Extra Stuff

To list all running applications:

pm2 list




Managing apps:

pm2 stop     <app_name|namespace|id|'all'|json_conf>

pm2 restart  <app_name|namespace|id|'all'|json_conf>

pm2 delete   <app_name|namespace|id|'all'|json_conf>

To have more details on a specific application:

pm2 describe <id|app_name>

To monitor logs, custom metrics, application information:

pm2 monit

Log Management

To consult logs just type the command:

pm2 logs

Standard, Raw, JSON and formated output are available.


Examples:

pm2 logs APP-NAME       # Display APP-NAME logs
pm2 logs --json         # JSON output
pm2 logs --format       # Formated output

pm2 flush               # Flush all logs
pm2 reloadLogs          # Reload all logs


Making Changes to the Configuration File

If you need to modify the configuration file, you will need to run these steps:

  1. End the current pm2 Service      
    pm2 stop all 
  2. Delete the current instances
    pm2 delete all

  3. Save the removal
    pm2 save

  4. Reload the config file.  From the c:\pm2home\ directory
    pm2 start ecosystem.config.js

  5. Save the new config instance
    pm2 save



Remove from Windows Services

Open command prompt (as Administrator) and run:     

pm2-service-uninstall


  • No labels