生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

今天介绍一个生产级的流程管理工具 PM2,通常我们说到 PM2 的时候,都是在说如何部署 Node.js 程序,但是实际上 PM2 很强大,不仅仅可以用来管理 Node.js,它还可以用来管理 Python、PHP、Ruby、perl 等等。

这里就以 Python 举例子,来看看 PM2 如何部署管理 Python 脚本。

有需要Python学习资料的小伙伴吗?小编整理【一套Python资料、源码和PDF】,感兴趣者可以加小编Python学习群:943752371反正闲着也是闲着呢,不如学点东西啦

PM2-Python

PM2 是一个生产级流程管理器,可以轻松管理后台进程,在 Python 的世界中,PM2 是可以和 Supervisord 相媲美的,并且 PM2 还有一些非常棒的功能。

使用 PM2,让崩溃重启、观察、检查日志甚至部署应用程序,都变的简单,并且 PM2 非常重视在命令行界面的操作体验,因此 PM2 非常易于使用和掌握。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

PM2 发展到今天,已经 5 年了,在 Github 上有超过 6500w 次下载,已经成为在生产服务器中运行 Node.js 的首选方式之一。但是它也支持 Python。

安装 PM2

PM2 依赖于 Node.js,所以需要提前安装 Node,这一步非常简单:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash –
sudo apt-get install -y nodejs
</pre>

其他平台如何安装 Node.js,可自行查找教程。

有了 Node 的环境后,就可以通过 npm 来安装 PM2 了。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ sudo npm install pm2 -g
</pre>

要初始化 PM2 ,可以使用 pm2 ls 命令,此时就可以看到一个很友好的界面。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

现在,已经成功安装好 PM2 了,让我们启动一个 Python 应用吧。

启动 Python

使用 PM2 启动应用非常的简单,它讲根据脚本扩展自动匹配解释器,用以运行指定的应用程序。

我们先创建一个简单的 Python 应用程序,例如:hello.py。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>#!/usr/bin/python
import time
while 1:
print(“Start: %s” % time.ctime())
time.sleep(1)
</pre>

我们有了一个简单的 Python 脚本,接下来我们就用 PM2 去启动它。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 start hello.py
</pre>

然后在 Terminal 里就可以看到该进程了。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

到这一步,这个 Python 程序就将永远的运行下去,这意味着如果该进程退出或者抛出异常,它将被自动重启。

此处的 mode 为 fork,也就是关闭当前的 Terminal 窗口,它依然可以检查到此应用的状态。

想要查看 PM2 运行管理的应用程序,可以使用 pm2 ls 命令进行查看。

检查日志

通过 PM2 运行的程序,如果想要查看 Log,可以输入 pm2 logs 命令。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

如果想要指定查看某个进程的 Log,可以使用 pm2 logs <app_name> 进行指定。

另外 PM2 还提供了自动化的日志轮换功能,但是需要安装 pm2-logrotate

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 install pm2-logrotate
</pre>

pm2-logrotate 将提供每天日志轮换更新的功能,并保持总的日志控件大小为 10M。

查看某进程的信息

想要查看当前使用 PM2 启动的程序的详细信息,可以使用 pm describe <app_name> 命令进行查看。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

在输出中,可以看到日志文件的路径,已经解释器等信息。

管理 PM2 的进程状态

介绍完启动和查看日志,再看几个简单的管理命令。

1. 停止某个程序

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 stop hello
</pre>

2. 重启某个程序

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 restart hello
</pre>

3. 从进程列表中停止和删除某个程序

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 delete hello
</pre>

更多的命令,可以直接查看官方文档。

服务器重启时,依然保持运行

使用 PM2 启动 Python 程序之后,PM2 只能保证启动的这个 Python 程序发生意外崩溃的时候,对他进行重启。如果你希望在重启服务器的时候,依然保持应用程序在线,则需要设置 init 脚本,用以告诉系统启动 PM2 以及你的应用程序。

想让 PM2 跟随系统启动,只需要运行此命令。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 startup
</pre>

startup 可以生成一个设置环境变量的命令。

æ�ªå�¾ – ä”� – 2018-09-19-13-05-39

复制/粘贴此命令的最后一行,执行后将在系统重启时,自动启动 PM2。

现在已经可以重启 PM2 了,还需要告诉 PM2 那些进程状态需要在重启时保持不变,只需要输入命令:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 save
</pre>

这将创建一个转存文件,记录当前由 PM2 管理的进程状态,PM2 将在重启时,按照之前的状态恢复他们。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

监听 CPU/内存信息

要监听 CPU/内存并检查有关进程的一些信息,需要使用 pm2 monit 命令。

这将打开一个 termcaps 界面,允许试试了解正在运行的应用程序。

《生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定》 生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定

你还可以使用 pm2 show <app_name> 获取有关应用程序的所有可能信息。

使用 Ecosystem 文件

如果有多个程序需要启动,或者在启动的时候需要传递不同的参数、选项等,可以使用 eocsystem 文件对应用程序进行配置。

Eocsystem 需要通过 ecosystem.config.js 文件进行配置,此文件可以通过 pm2 init 命令生成。生成好后,我们可以在其中配置一些配置信息。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>module.exports = {
apps : [{
name: ‘echo-python’,
cmd: ‘hello.py’,
args: ‘arg1 arg2’,
autorestart: false,
watch: true,
pid: ‘/path/to/pid/file.pid’,
instances: 4,
max_memory_restart: ‘1G’,
env: {
ENV: ‘development’
},
env_production : {
ENV: ‘production’
}
}, {
name: ‘echo-python-3’,
cmd: ‘hello.py’,
interpreter: ‘python3’
}]
};
</pre>

在这个例子中,我们声明了两个应用程序,通过 interpreter 配置程序启动的解释器,一个使用 Python2 (默认)运行,另一个使用 Python3 运行。

启动它,依然使用 pm2 start 命令。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 start ecosystem.config.js
</pre>

想要单独重启 “production” (env_production):

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 restart ecosystem.config.js –env production
</pre>

Ecosystem.config.js 文件中,很多配置都是可以通过命令来指定,例如,可以通过 –interpreter 来指定解析程序。

通常我们会同时安装 Python2.x 和 Python3.x 的环境,而 PM2 在默认情况下,是通过脚本文件后缀来判断的,如果没有后缀就需要强制指定 –interpreter 。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>{
“.sh”: “bash”,
“.py”: “python”,
“.rb”: “ruby”,
“.coffee” : “coffee”,
“.php”: “php”,
“.pl” : “perl”,
“.js” : “node”
}
</pre>

这些配置信息也标记了 PM2 支持的脚本程序。

那么如果需要使用 Python3.x 来执行某个脚本,就需要 –interpreter 了。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pm2 start hello.py –interpreter=python3
</pre>

小结

PM2 的简单使用,就先介绍到这里。虽然这里使用 Python 来举例,但是本文所有相关命令,是可以适用其他 PM2 支持的脚本程序。

PM2 还有很多强大的功能,比如说利用 SSH 轻松部署到服务器、负载均衡等等都是一些不错的功能,有兴趣可以查阅文档。PM2 文档很健全,大部分问题都可以在文档中找到答案。

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/609d788a0f28
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞