Deployment method of nodejs-PM2

Source: Internet
Author: User

Deployment method of nodejs-PM2
Today, my colleague sitting on the right suddenly told me with joy: Hey, we recommend you try PM2, which is much higher than forever. I have always been behind the cutting edge of fashion technologies? PM2.5 is so serious, PM2 is coming again! If it weren't all used to my second career, the atmosphere would have to be around for a while ~~ Haha ...... First, let's talk about how I usually deploy a program. 1. The most common one is nohup. In fact, it is to execute the process in the background and add a process at the end. & [zhoujie @ ops-dev ~] $ Nohup node/home/zhoujie/ops/app. js & [1] 31490 nohup: ignoring input and appending output to 'nohup. 'out' indicates that the program has been started and can be accessed directly. The standard output of the original program is automatically changed to nohup in the current directory. the out file plays the role of log. This command can continue running the corresponding process after you exit the account/Close the terminal. Nohup means no hang up ). The general form of this command is: nohup command & this is not very reliable, often silent processes are hung in the background 2. Use screen to open another screen, in this way, you can directly view the program running status on the screen and open a screen for the application, such as screen-r ops. start with npm start and exit the background: ctrl +, press "d" instead of "ctrl + c". Otherwise, it is very unprofessional to exit this method, but it is convenient to operate in the production environment. This is essentially a forever, package. "scripts": {"start": "forever app. js "," test ":" supervisor app. js "}, 3. Use PM2 to install it first, and use the root account and global mode to install it: npm install-g pm2 uses it to start the program (you can directly start it in the current directory, pm2 start app. js -- name uops) [zhoujie @ ops-dev uops] $ pm2 start app. js [PM2] Spawning PM2 daemon [PM2] Success [PM2] Process app. js launched ── ─ ── ─ ── │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ── ─ ── ─ %│ app │ 0 │ fork │ 308 │ online │ 0 │ 0 s │ 21.879 MB │ disabled │ └ ── ─ ── ─ Use 'pm2 info <id | name> 'to get more details about an app [zhoujie @ ops- dev uops] $ view, it displays Success is displayed, and the program has been started silently. You can monitor the running of the program in real time. For example, if you execute a pm2 restart, the restarted column is changed to 1, it shows how long the program has been running and the memory size is occupied. This is awesome! Terminating the program is also simple: pm2 stop lists all programs started with pm2: pm2 list [zhoujie @ ops-dev uops] $ pm2 list ┌ ── ─ ── ─ %│ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ├ ── ─ ── ─ ── │ app │ 0 │ fork │ 984 │ online │ 1 │ 3 s │ 64.141 MB │ disabled │ └ ── ─ ┴ ── ─ ── Just Use 'pm2 info <id | name> 'to get more details about an app to view the Startup Program details: pm2 describe id copy code [zhoujie @ ops-dev uops] $ pm2 desc 0 Describing process with pid 0-name app ── ─ ── ─ ── ─│ status │ online │ name │ app │ id │ 0 │ path │/home/ zhoujie/uops/app. js │ args │ exec cw D │/home/zhoujie/uops │ error log path │/home/zhoujie /. pm2/logs/app-error-0.log │ out log path │/home/zhoujie /. pm2/logs/app-out-0.log │ pid path │/home/zhoujie /. pm2/pids/app-0.pid │ mode │ fork_mode │ node v8 arguments │ watch & reload │ interpreter │ node │ restarts │ 1 │ unstable restarts │ 0 │ │ uptime │ 93 s │ created at │ 2015-01-07T09: 41: 25.672Z │ └ ── ─ ┴ ── ── ─ Running [zhoujie @ ops-dev uops] $ use the pm2 list command to observe all running processes and their the status is good enough. but how can we track their resource consumption? Don't worry. Run this command: pm2 monit to get the CPU usage and memory usage of processes (and clusters) (ctrl + c Exit). Real-time centralized log processing: powerful API of pm2 logs: pm2 web: You want to monitor all processes managed by PM2 and the status of the machines running these processes, copy the Code [zhoujie @ ops-dev uops] $ pm2 webLaunching web interface on port 9615 [PM2] Process/usr/local/node/lib/node_modules/pm2/lib/HttpInterface. js launched [PM2] Process launched ── ─ -┬ ── ─ ┐ │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ── ─ ── ─ ── │ app │ 0 │ fork │ 984 │ online │ 1 │ 9 m │ 74.762 MB │ disabled │ pm2-http-interface │ 1 │ fork │ 1878 │ online │ 0 │ 0 │ 0 s │ 15.070 MB │ disabled │ └ ── ─ ── ─ ── ─ ┴ ── ─ Use 'pm2 info <id | name> 'to get more details about an app to copy the code to start the program, By the way in the browser access: http: // localhost: 9615 wipe, my eyes are blind, so cool, even the deployed server information and program information are displayed: this is very helpful for the development of the monitoring page for running programs ~~ Okay, I admit that I am a little out of control, and I am a little confused. I am willing to repeat the command again: copy the code to install: npm install-g pm2 to start the program: pm2 start <app_name | id | all> list process: pm2 list exit program: pm2 stop <app_name | id | all> restart application: pm2 restart program information: pm2 describe id | all monitoring: pm2 monit real-time centralized log processing: pm2 logsAPI: pm2 web (Port: 9615)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.