PM2 is a production-level process management tool. When we talk about PM2, we usually talk about how to deploy Node.js applications, but PM2 is actually very powerful and can be used for more than just managing Node.js. It can also be used to manage Python, PHP, Ruby, Perl, and so on.

Using Python as an example, see how PM2 deploys and manages Python scripts.

PM2-Python

The PM2 is a production-grade process manager that makes it easy to manage background processes, it’s comparable to the Python world it’s designed for, and it’s also equipped with some great features.

PM2 makes it easy to crash, restart, observe, check logs, and even deploy applications, and its emphasis on the command-line interface makes PM2 easy to use and master.

PM2 is now five years old, with over 6,500m downloads on Github, and has become one of the preferred ways to run Node.js on production servers. But it also supports Python.

Install the PM2

PM2 relies on Node.js, so you need to install Node in advance. This step is very simple:

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

sudo apt-get install -y nodejs

Copy the code

Find tutorials on how to install Node.js on other platforms.

With the Node environment in place, you can install PM2 using NPM.

$ sudo npm install pm2 -g

Copy the code

To initialize PM2, you can use the PM2 ls command, which gives you a very friendly interface.

Now that PM2 has been successfully installed, let’s launch a Python application.

Start the Python

Launching an application with PM2 is very simple, and it automatically matches the interpreter according to the script extension to run the specified application.

Let’s start by creating a simple Python application, such as hello.py.

#! /usr/bin/python

import time



while 1:

    print("Start: %s" % time.ctime())

    time.sleep(1)

Copy the code

Now that we have a simple Python script, let’s start it with PM2.

$ pm2 start hello.py

Copy the code

Then you can see the process in Terminal.

At this point, the Python program will run forever, which means that if the process exits or throws an exception, it will be restarted automatically.

The mode here is fork, that is, close the current Terminal window, which can still check the status of the application.

To view the applications that PM2 runs and manages, use the PM2 ls command.

Check the log

For programs running through PM2, if you want to view logs, you can type the PM2 logs command.

If you want to specify the logs to view for a process, you can specify pM2 logs

.

PM2 also provides automatic log rotation, but requires installation of PM2 – Logrotate

$ pm2 install pm2-logrotate

Copy the code

Pm2 – Logrotate will provide daily log rotation and keep the total log control size at 10M.

View information about a process

To see details of the program currently started using PM2, use the PM describe

command.

In the output, you can see the path of the log file, the interpreter, and so on.

Manage PM2 process status

After starting and viewing logs, look at a few simple management commands.

1. Stop a program

$ pm2 stop hello

Copy the code

2. Restart a program

$ pm2 restart hello

Copy the code

3. Stop or delete a program from the process list

$ pm2 delete hello

Copy the code

For more commands, you can view the official documentation directly.

When the server restarts, it remains running

After starting a Python program using PM2, PM2 can only ensure that the started Python program is restarted in the event of an unexpected crash. If you want to keep your application online while restarting the server, you need to set up the init script that tells the system to start PM2 and your application.

To start PM2 with the system, run this command.

$ pm2 startup

Copy the code

Startup generates a command to set environment variables.

截 A ›¾ -a “Ž – 2018-09-19-13-05-39

Copy/paste the last line of this command. The PM2 automatically starts when the system restarts.

Now that you are ready to restart PM2, you need to tell PM2 which process states need to remain the same during restart by typing:

$ pm2 save

Copy the code

This will create a dump file that records the status of the processes currently managed by PM2, and PM2 will restore them to their previous state upon restart.

Listen for CPU/ memory information

To listen on CPU/ memory and check some information about the process, use the pm2 monit command.

This will open a TermCaps interface, allowing you to try out the running application.

You can also use pm2 show

to get all possible information about your application.

Using the Ecosystem file

If you have multiple programs that need to be started, or you need to pass different parameters, options, and so on during startup, you can configure your application using the eOCSystem file.

Eocsystem needs to be configured in the file.config. js file, which can be generated by using the pm2 init command. Once generated, we can configure some configuration information in it.

module.exports = {

  apps : [{

    name'echo-python'.

    cmd'hello.py'.

    args'arg1 arg2'.

    autorestartfalse.

    watchtrue.

    pid'/path/to/pid/file.pid'.

    instances4.

    max_memory_restart'1G'.

    env: {

      ENV'development'

    },

    env_production : {

      ENV'production'

    }

  }, {

    name'echo-python-3'.

    cmd'hello.py'.

    interpreter'python3'

  }]

};

Copy the code

In this example, we declare two applications, an interpreter started through the Interpreter configuration program, one running using Python2 (the default) and the other running using Python3.

To start it, use the pm2 start command again.

$ pm2 start ecosystem.config.js

Copy the code

Want to restart production separately (env_production) :

$ pm2 restart ecosystem.config.js --env production

Copy the code

Many of the configurations in the file.config.js file can be specified using commands. For example, you can specify a parser through –interpreter.

Typically we install python2. x and python3. x environments at the same time, and PM2 by default is determined by the script file suffix, which is enforced if there is no suffix –interpreter.

{

  ".sh""bash".

  ".py""python".

  ".rb""ruby".

  ".coffee" : "coffee".

  ".php""php".

  ".pl" : "perl".

  ".js" : "node"

}

Copy the code

This configuration information also flags PM2 supported scripts.

So if you need python3. x to execute a script, you need –interpreter.

$ pm2 start hello.py --interpreter=python3

Copy the code

summary

The simple use of PM2 is introduced here. Although the Python example is used here, all of the commands in this article are scripts that can be used with other PM2 support.

PM2 also has many powerful features, such as easy deployment to servers using SSH, load balancing, and so on are some nice features, if you are interested in the documentation. PM2 documentation is robust and answers to most questions can be found in documentation.

If you have any questions, please feel free to share them in the comments section. Thank you!

Reference:

https://blog.pm2.io/managing-python-application-with-pm2

https://pm2.io/doc/en/runtime/quick-start/

Public number background reply growth “growth”, will get the learning materials I prepared, can also reply “add group”, learning progress together; You can also reply to “questions” and ask me questions.

Recommended reading:

| using pretreatment script illustrations Chrome, architecture, management | small program code word segmentation, popular science and solutions | illustration: HTTP request scope | small program learning materials | | HTTP content-type auxiliary mode of actual combat | aided the tricks | small program Flex layout