PM2 running json file and pm2-web configuration

Keywords: node.js JSON Linux npm zlib

PM2 runs json file to start node project and pm2-web installation

Define the run configuration file

We can predefine a configuration file and then work out how to run it. For example, we can define a file app.json, which reads as follows:

[root@jenkins-master linux]# cat app.json   
{
"apps":[
    {
      "name": "service-sk_platform_professional_dev",
      "cwd": "/data/source/service-sk_platform_professional_dev/publish/",
      "script": "/data/source/service-sk_platform_professional_dev/publish/service/server.js",
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "error_file": "/data/pm2-log/errlogs/service-sk_platform_professional_dev-err.log",
      "out_file": "/data/pm2-log/outlogs/service-sk_platform_professional_dev-out.log",
      "pid_file": "/data/pm2-log/outlogs/service-sk_platform_professional_dev.pid",
      "instances": 1,
      "node_args": "--inspect=0.0.0.0:33111",
      "min_uptime": "20s",
      "max_restarts": 50,
      "watch": false,
      "merge_logs": true,
      "exec_interpreter": "node",
      "exec_mode": "fork",
    }
    ]
}
[root@node-44 ~]# cat app.json 
{
  "apps":[
    {
      "name": "sk_service_calculate_convert_dev",
      "cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
      "script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert_dev-err.log",
      "out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert_dev-out.log",
      "pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert_dev.pid",
      "instances": 1,
      "min_uptime": "20s",
      "max_restarts": 50,
      "watch": false,
      "merge_logs": true,
      "exec_interpreter": "node",
      "exec_mode": "fork",
},{
      "name": "sk_service_calculate_convert_dev2",
      "cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
      "script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
      "args": "'debug' '12345'",
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert2_dev-err.log",
      "out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert2_dev-out.log",
      "pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert2_dev.pid",
      "instances": 1,
      "min_uptime": "20s",
      "max_restarts": 50,
      "watch": false,
      "merge_logs": true,
      "exec_interpreter": "node",
      "exec_mode": "fork",
  },{
      "name": "sk_service_calculate_convert_dev3",
      "cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
      "script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
      "args": "'debug' '12344'",
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert3_dev-err.log",
      "out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert3_dev-out.log",
      "pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert3_dev.pid",
      "instances": 1,
      "min_uptime": "20s",
      "max_restarts": 50,
      "watch": false,
      "merge_logs": true,
      "exec_interpreter": "node",
      "exec_mode": "fork",
  },{
      "name": "sk_service_calculate_convert_dev4",
      "cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
      "script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
      "args": "'debug' '12343'",
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert4_dev-err.log",
      "out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert4_dev-out.log",
      "pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert4_dev.pid",
      "instances": 1,
      "min_uptime": "20s",
      "max_restarts": 50,
      "watch": false,
      "merge_logs": true,
      "exec_interpreter": "node",
      "exec_mode": "fork",
  }
]
}
for (int i = 0;i<"${node_number}".toInteger();i++){
                     Integer nodeport=10000+i
                        sh "cat /data/ansible/playbook/linux/linux_app.json |sed -e 's/sourcepath_node_Jenvironment/${projectname}_${environment}_${i}/g' -e 's/sourcepath_Jenvironment/${projectname}_${environment}/g' -e 's/process_mode/${process_mode}/g' -e 's/node_port/${nodeport}/g' >>${WORKSPACE}/publish/app.json"
                        sh(script: "echo -n ',\r\n' >>${WORKSPACE}/publish/app.json")
                    
                    }
  • Then you can run the App by running it.

pm2 start app.json

apps:json Structure, apps It's an array, and each group member corresponds to one. pm2 Application in Operation  
name:Application Name  
args:Parametric fields of scripts
cwd:Directory where the application is located  
script:Application script path  
log_date_format:
node_args:node Parameter domain  
error_file:Error log files for custom applications  
out_file:Custom application log file  
pid_file:Custom application pid file  
instances:  
min_uptime:Minimum run time, set here at 60 s That is, if the application is in 60 s Internal withdrawal pm2 The program will be considered to exit abnormally, triggering a restart max_restarts Set quantity  
max_restarts:Set the number of times the application abnormally exits and restarts by default of 15 (counting from 0)  
cron_restart:Timing start to solve problems that can be solved by restart  
watch:Whether monitoring mode is enabled by default is false. If set to true,When the application changes, pm2 It will automatically overload. Here you can also set up the files you want to monitor.  
merge_logs:  
exec_interpreter:The script type of the application, which is used here shell,The default is nodejs  
exec_mode:Application startup mode, set here is cluster_mode(Cluster, by default fork  
autorestart:Enable/Disable automatic restart when application crashes or exits  
vizion:Enable/Prohibit vizion Characteristic(version control)

Summary - Common commands are summarized as follows:

  • Install pm2

npm install -g pm2

  • Startup application

pm2 start app.js

  • List all applications

pm2 list

  • View resource consumption

pm2 monit

  • View an application status

pm2 describe [app id]

  • View all logs

pm2 logs

  • restart app

pm2 restart [app id]

  • Stop application

pm2 stop [app id]

  • Open api access

pm2 web

Install pm2-web

Installation software

Installation requirements g++,make Wait for the software, the following software you choose (I just installed the system installed)

yum -y install ntpdate lrzsz tree cmake gcc gcc-c++ autoconf l libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel libxslt-devel libtool-ltdl-devel make wget docbook-dtds asciidoc e2fsprogs-devel gd gd-devel openssl openssl-devel lsof git unzip gettext-devel gettext libevent libevent-devel pcre pcre-devel

npm install --unsafe-perm -g pm2-web

  
pm2-web

Configuration startup file

[root@localhost ~]# cat pm2-web-config.json 

{  
    "www": {  
        "host": "localhost",  
        "address": "0.0.0.0",  
        "port": 11111  
    }                           
}

[root@localhost ~]# cat pm2-web-start.sh

  
nohup pm2-web --config pm2-web-config.json >pm2-web.log&

  
[root@localhost ~]# sh +x   pm2-web-start.sh

server {  
        listen 80;  
        server_name pm2-web.xxxx.com;  
        access_log /data/nginx/logs/node-web.xxx.com/node-web.xxxx.com.log main;  
        location / {  
                auth_basic              "node-web monitor";                                                                        
                auth_basic_user_file    /data/nginx/htpasswd.conf;  //Setting User Access Control  
                proxy_pass http://192.168.0.xxx:xxxx;  
                proxy_redirect off;  
                proxy_set_header Host $http_host;  
                proxy_set_header X-Real-IP $remote_addr;  
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
                proxy_http_version 1.1;  
                proxy_set_header Upgrade $http_upgrade;  
                proxy_set_header Connection "upgrade";  
                proxy_connect_timeout 360;  
                proxy_send_timeout 360;  
                proxy_read_timeout 360;  
        }  
}

Posted by dpluth on Sat, 05 Oct 2019 03:41:28 -0700