• pm2 设置开机启动


    一、官方文档:

    官方相关文档:http://pm2.keymetrics.io/docs/usage/startup/#generating-a-startup-script

    二、具体操作过程如下:

    1、生成开机启动 pm2 服务的配置文件

    运行:pm2 startup 命令

    ➜  ~ pm2 startup
    [PM2] Init System found: systemd
    [PM2] To setup the Startup Script, copy/paste the following command:
    sudo env PATH=$PATH:/home/user1/.nvm/versions/node/v8.8.1/bin /home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2
    startup systemd -u user1 --hp /home/user1

    粘贴上面的命令到终端,如下:

    ➜  init.d sudo env PATH=$PATH:/home/user1/.nvm/versions/node/v8.8.1/bin /home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2
    startup systemd -u user1 --hp /home/user1 [sudo] user1 的密码:

    在非root用户下会让你输入密码。密码输入完成后终端会出现如下内容:

    PM2] Init System found: systemd
    Platform systemd
    Template
    [Unit]
    Description=PM2 process manager
    Documentation=https://pm2.keymetrics.io/
    After=network.target
    
    [Service]
    Type=forking
    User=user1
    LimitNOFILE=infinity
    LimitNPROC=infinity
    LimitCORE=infinity
    Environment=PATH=/home/user1/.nvm/versions/node/v8.8.1/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    Environment=PM2_HOME=/home/user1/.pm2
    PIDFile=/home/user1/.pm2/pm2.pid
    
    ExecStart=/home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2 resurrect
    ExecReload=/home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2 reload all
    ExecStop=/home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2 kill
    
    [Install]
    WantedBy=multi-user.target
    
    Target path
    /etc/systemd/system/pm2-user1.service
    Command list
    [ 'chmod +x /etc/systemd/system/pm2-user1.service',
      'systemctl enable pm2-user1',
      'systemctl start pm2-user1',
      'systemctl daemon-reload',
      'systemctl status pm2-user1' ]
    [PM2] Writing init configuration in /etc/systemd/system/pm2-user1.service
    [PM2] Making script booting at startup...
    >>> Executing chmod +x /etc/systemd/system/pm2-user1.service
    [DONE]
    >>> Executing systemctl enable pm2-user1
    Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-user1.service to /etc/systemd/system/pm2-user1.service.
    [DONE]
    >>> Executing systemctl start pm2-user1
    [DONE]
    >>> Executing systemctl daemon-reload
    [DONE]
    >>> Executing systemctl status pm2-user1
    ● pm2-user1.service - PM2 process manager
       Loaded: loaded (/etc/systemd/system/pm2-user1.service; enabled; vendor preset: disabled)
       Active: active (running) since �� 2018-02-09 13:17:38 CST; 47ms ago
         Docs: https://pm2.keymetrics.io/
     Main PID: 24295 (PM2 v2.7.2: God)
       CGroup: /system.slice/pm2-user1.service
               ‣ 24295 PM2 v2.7.2: God Daemon (/home/user1/.pm2)
    
    2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz systemd[1]: Starting PM2 process manager...
    2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz pm2[6857]: [PM2] Resurrecting
    2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz systemd[1]: Started PM2 process manager.
    [DONE]
    +---------------------------------------+
    [PM2] Freeze a process list on reboot via:
    $ pm2 save
    
    [PM2] Remove init script via:
    $ pm2 unstartup systemd

    按照提示的命令进行配置就好了。你会在/etc/systemd/system文件夹下看到pm2-user1.service文件

    2、pm2 save, 保存当前 pm2 运行的各个应用保存到 /home/user1/.pm2/dump.pm2下,开机重启时读取该文件中的内容启动相关应用。

    ➜  ~ pm2 save
    [PM2] Saving current process list...
    [PM2] Successfully saved in /home/user1/.pm2/dump.pm2

    3、查看配置是否开启:使用命令:systemctl status pm2-user1.service

    ➜  system systemctl status pm2-user1.service
    ● pm2-user1.service - PM2 process manager
       Loaded: loaded (/etc/systemd/system/pm2-user1.service; enabled; vendor preset: disabled)
       Active: active (running) since �� 2018-02-09 13:17:38 CST; 44min ago
         Docs: https://pm2.keymetrics.io/
     Main PID: 24295 (PM2 v2.7.2: God)
       CGroup: /system.slice/pm2-user1.service
               ‣ 24295 PM2 v2.7.2: God Daemon (/home/user1/.pm2)
    
    2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz pm2[6857]: [PM2] Resurrecting

    参考:http://imweb.io/topic/57c8cbb27f226f687b365636

    http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

  • 相关阅读:
    微信小程序通过getPhoneNumber后台PHP解密获取用户手机号码
    设置API:wx.openSetting,wx.getSetting使用说明(示例:地图授权与取消授权后的重新授权)
    微信小程序之上传图片和图片预览
    微信小程序上传图片(前端+PHP后端)
    微信小程序拒绝授权后重新拉起授权窗口
    微信小程序如何使用 Git 实现版本管理和协作开发
    配置同时使用 Gitlab、Github、Gitee(码云) 共存的开发环境
    linux 系统下Anaconda的安装【安装python3.6环境首选】
    利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)
    利用phpqrcode二维码生成类库和imagecopymerge函数制拼接图片的经验(一)
  • 原文地址:https://www.cnblogs.com/duhuo/p/5587247.html
Copyright © 2020-2023  润新知