Process description file

Ecosystem File

Ecosystem files, also known as process description files, allow you to adjust the behavior, options, environment variables, logs, and so on of each process through the files, which can be very helpful for microservices-based applications

The supported configuration formats are Javascript, JSON, and YAML

PM2 instance files can be generated through Clipm2 init or PM2 ecosystem in node module format. Examples are as follows

module.exports = {
  apps: [{name: 'API'.// Application name
      script: 'app.js'.// Execute the script

      // Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
      args: 'one two'.// Start parameters
      instances: 1.// Number of instances. PM2 has built-in load balancing. Multiple instances can be enabled
      autorestart: true.// Automatic restart
      watch: false.// Whether to listen for file changes
      max_memory_restart: '1G'.// Restart the application when the maximum memory threshold is reached
      // Run the environment
      env: {
        NODE_ENV: 'development'
      },
      env_production: {
        NODE_ENV: 'production'}},// Process description of the second application{}].deploy : {
    production : {
      user : 'node'.host : '212.83.163.1'.ref  : 'origin/master'.repo : '[email protected]:repo.git'.path : '/var/www/production'.'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'}}};Copy the code

It is obvious that multiple processes can be configured at once

After editing the process description file, you can start using the Cli. Note that the Javascript configuration ends in.config.js

pm2 [start|restart|stop|delete] ecosystem.config.js
Copy the code

Parameters can be used to adjust the startup action. The following describes specific parameters

  • –only

    Start only the application with the specified name. Multiple applications can be specified
    ,…>
  • Env allows you to switch the operating environment of a system, which is useful in deployment test, pre-production, production and other scenarios that require environment differentiation. For example,

The complete parameters are as follows

| - based parameter name: application nameString| (app) script: the script pathString(app. Js) CWD: working directory |String (/var/| WWW) args: parameterArray.String(env) --, port -- interpreter: | script interpreter pathString (/var/jre/java, /var/ python/python) interpreter_args: the parameters of the script interpreter |Array(-xmx) -- advanced parameters instances: instances | number (1.2.3...) | exec_mode: execution wayString(the fork, cluster) watch: start monitoring a file or directory changes automatically restart | Boolean (true.false) ignore_watch refers to watch | need to ignore the directory or fileArray.String(node_modules) max_memory_restart: restart the largest memory threshold |String (150M,200M) env: | environmental variablesObject ({NODE_ENV:'dev'.port: 8080}) source_map_support: source support | Boolean (true.false|) instance_var: instance variablesString| filter_env: filtering operation environmentArray (["REACT_"]), log log_date_format: log date format | Strin (yyyy - MM - dd) error_file: | error file pathString (/var/Logs/xxxerror. Log) out_file: | log output pathString (/var/Logs/XXX. The log) combine_logs: combination logging | Boolean (true.false) merge_logs: combine log | Boolean (true.false|) pid_file: pid file pathString (/var/XXX/app. Pid) -- -- control related min_uptime: minimum uptime |StringListen_timeout: listening to the startup delay | number how many milliseconds later enabled to monitor kill_timeout: close the process delay | shutdown_with_message number how many milliseconds after closing process: Closed by a message | Boolean pm2 through the process. The send () 'shutdown' notifications, has shut down by the application control wait_ready: pm2 waiting send application process. The send () 'ready' start max_restarts: Maximum continuous reboot times | number/restart the largest number, more than try to start the application no longer restart_delay: restart delay | number how many milliseconds later restart delay perform autorestart: automatic restart | Boolean (true.false) cron_restart: restart | regularlyStringYou can use cron expressions to control the application restart timeCopy the code

Manage non-Node processes

See here if you are careful, you must find. It seems that PM2 can do more than manage and guard NODE applications.

PM2 can actually execute processes across languages such as JAVA. Here is a small example of managing Eureka through PM2

  • Start using the CLI
pm2 start java -- -jar eureka.jar --server.port=8088 --name=eureka
Copy the code
  • Through the process description file
module.exports = {
  name: 'eureka'.// Application name
  /** * To execute the script, note that non-Node applications need to execute the interpreter specified in script * example specify the full path, if the environment variable has been configured directly to specify Java */
  script: '/ Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/bin/Java'./** * start arguments * execute eureka.jar * listen on port 8088 */
  args: '-jar eureka.jar --server.port=8088'
}

Copy the code

After the script is executed, you can run the pm2 status or pm2 list command to check the application health status

Or more intuitively, it can be monitored in real time through the PM2 built-in monitor

PM2 PLUS

The PM2 team also launched PM2 PLUS, an online management platform that allows you to easily check the running status of the PM2 process. The free version provides remote restart, stop, and log viewing functions

The Professional and Business editions support remote builds online

The resources

Ecosystem-File

PM2-plus