pm2启动python应用-实例

server.py代码段修改
  • server.py路径:/data/source/projectname_env/src
import os,sys  
from ailayout import settings  
src_dir = os.path.dirname(os.path.realpath(__file__))  
settings.WORKDIR = os.path.dirname(src_dir)

########################添加port1变量####################  
try:  
    port1 = sys.argv[1]  
except IndexError:  
    port1 = False
############当port1为空时设置port1为:False###############
  
from ailayout.server import app  
def main():  
    # app.debug =True  
    if sys.platform == 'win32':  
        # app.run(host='127.0.0.1', port=80)  
        app.run(host='0.0.0.0', port=80)  
    else:  
################判断port1是否问True###################
        if port1:  
            app.run(host='0.0.0.0', port=port1)  
        else:  
            app.run(host='0.0.0.0', port=5001)
#####################################################
if __name__ == '__main__':  
    main()

python应用pm2-app.json配置


{
    "apps": [
        {
            "args": "5001",
            "cwd": "/tmp/ailayout_std",
            "error_file": "/data/pm2-log/errlogs/ailayout-err1.log",
            "exec_interpreter": "/usr/bin/python",
            "exec_mode": "fork",
            "instances": 1,
            "log_date_format": "YYYY-MM-DD HH:mm Z",
            "max_restarts": "50",
            "merge_logs": true,
            "min_uptime": "20s",
            "name": "ailayout_std1",
            "out_file": "/data/pm2-log/outlogs/ailayout_std-out1.log",
            "pid_file": "/data/pm2-log/outlogs/ailayout_std1.pid",
            "script": "/tmp/ailayout_std/src/server.py",
            "watch": false
        },
        {
            "args": "5002",
            "cwd": "/tmp/ailayout_std",
            "error_file": "/data/pm2-log/errlogs/ailayout-err2.log",
            "exec_interpreter": "/usr/bin/python",
            "exec_mode": "fork",
            "instances": 1,
            "log_date_format": "YYYY-MM-DD HH:mm Z",
            "max_restarts": "50",
            "merge_logs": true,
            "min_uptime": "20s",
            "name": "ailayout_std2",
            "out_file": "/data/pm2-log/outlogs/ailayout_std-out2.log",
            "pid_file": "/data/pm2-log/outlogs/ailayout_std2.pid",
            "script": "/tmp/ailayout_std/src/server.py",
            "watch": false
        },
        {
            "args": "5003",
            "cwd": "/tmp/ailayout_std",
            "error_file": "/data/pm2-log/errlogs/ailayout-err3.log",
            "exec_interpreter": "/usr/bin/python",
            "exec_mode": "fork",
            "instances": 1,
            "log_date_format": "YYYY-MM-DD HH:mm Z",
            "max_restarts": "50",
            "merge_logs": true,
            "min_uptime": "20s",
            "name": "ailayout_std3",
            "out_file": "/data/pm2-log/outlogs/ailayout_std-out3.log",
            "pid_file": "/data/pm2-log/outlogs/ailayout_std3.pid",
            "script": "/tmp/ailayout_std/src/server.py",
            "watch": false
        }

    ]    
}
"script": "/tmp/ailayout_std/src/server.py" ##需要执行的python文件
"exec_interpreter": "/usr/bin/python" ##python的环境文件
"args": "5003" ###是server.py 的参数

锅包肉
89 声望17 粉丝

这个人很懒,没有什么说的。