LINUX-PM2 usage

Source: Internet
Author: User
Tags cpu usage keep alive

The DEVO.PS team's obsession with JavaScript is no secret; node. js as the server side, Angularjs as the client, in a way, our stack is built with it. We build static clients and restful JSON The API approach means that we run a lot of node. js, and I have to admit that although all of node. JS is awesome, it still makes us headache when we run it in a production environment. Its tools and best practices are still scarce compared to some more mature languages (imagine: monitoring, logging, Error handling).

So far, we still rely on pretty playful node-forever modules. It's a great module, but it's still missing some features:

    • Limited monitoring and logging capabilities,
    • Poor support for process management configuration,
    • Cluster is not supported,
    • Code base Aging (which means frequent failures when you upgrade node. js).

That's why we're going to write the PM2 module in the last few months. We'd like to give you a look first before we release the official version of the production environment.

What the hell is a PM2?

First thing first, you need to install it with NPM first:

NPM install-g PM2

Let's compare the table to the following:

Feature Forever PM2
Keep Alive ? ?
Coffeescript ?
Log Aggregation ?
Api ?
Terminal Monitoring ?
Clustering ?
JSON Configuration ?

Now let me introduce a little bit of the main features ...

Native Clustering support

Node v0.6 introduces cluster features that allow you to share sockets across multiple node apps. The problem is that it doesn't run outside the container and requires some extra configuration to handle the main and child processes.

PM2 Native support handles this problem and does not require additional code: PM2 itself as the main process, then it encapsulates your code into a special cluster process, like node. js, adding some global variables to your code files.

To start a cluster that uses all CPU cores, you only need to type the following instructions:

$ PM2 Start App.js-i max

and then

$ PM2 List

Then it will show something like the following (ASCII UI FTW);

As you can see, the number of processes in your app now depends on your CPU cores.

Managed according to Termcaps-htop (System monitoring and process management software under Linux)

It's good enough to see all the running processes and their states through the PM2 List command. But how do you track their resource consumption? Don't worry, use this command:

$ PM2 Monit

You can get the CPU usage and memory footprint of the process (as well as the cluster).

Disclaimer : Node-usage does not support MacOS (any performance requirement) so far, but it works well under Linux.

Now, let's check out our cluster and the garbage collection on the memory stack, assuming you already have an HTTP benchmark tool (if not, you must use WRK):

$ express Bufallo//Create an express app $ cd bufallo$ npm install$ pm2 start app.js-i max$ wrk-c 100-d Calhost:3000/

In another terminal, run the monitoring option:

$ PM2 Monit

Yes ~

Real-time Centralized log processing

Now you have to manage multiple cluster processes: One to crawl data, one to process data, and so on ... This means a lot of log, which you can deal with in an old-fashioned way:

$ tail-f/path/to/log1/path/to/log2 ...

But we thought very thoughtful, we added the logs function:

$ PM2 Logs

Fast Recovery

Now that things are going well, your process is buzzing and you need to do a hard restart (restart). Now? Yes, first of all, dump:

$ PM2 Dump

You can then restore it from the file:

$ PM2 kill//Let's assume a PM2 stopped.


Robust API

For example, you want to monitor all the processes that are managed by PM2, and you want to monitor the state of the machines that are running them (and even want to create a angular app to invoke these APIs ...):

$ PM2 Web

Open a browser input http://localhost:9615, I go!!

Yes, there are many features ...
    • All tests Pass,
    • A new generation of UPDATE-RC.D (PM2 startup), of course it's still alpha,
    • Change file auto restart (PM2 dev) in development mode, also draft,
    • Automatically refreshes log,
    • Quickly manage your apps with JSON files,
    • An uncaught exception is recorded in the error log.
    • Record the number and time of reboots,
    • Automatically kills processes when exiting.
Next plan?

First of all, you can go to GitHub to powder us (we like stars): HTTPS://GITHUB.COM/UNITECH/PM2.

The PM2 we develop provides an advanced and complete node process management solution. We hope to have more people to help us: more pull requests. Some of the features that are still on the development roadmap will be completed as soon as possible, and these are:

    • Remote Management/Status check,
    • Embedded cross-process communication channel (message bus),
    • V8 A memory leak check for garbage collection,
    • Web interface,
    • Monitoring of data persistence,
    • Mail notifications.

Especially thanks to Makara Wang for his ideas and tools, as well as the advice and code submitted by Alex Kocharin.

LINUX-PM2 usage

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.