当前位置: 首页 > 工具软件 > PM Report > 使用案例 >

PM2区分环境启动

家浩瀚
2023-12-01

新建ecosystem.config.js

module.exports = {
  apps: [
    {
      name: 'huike-cms',
      script: 'server/index.js',
      env: {
        NODE_ENV: 'production',
        RUN_ENV: 'production',
      },
      env_prod: {
        NODE_ENV: 'production',
        RUN_ENV: 'production',
      },
      env_test: {
        NODE_ENV: 'test',
        RUN_ENV: 'test',
      },
      watch: ['server'],
      ignore_watch: ['node_modules', 'logs', 'build'],
      cwd: './',
    },
  ],
}

参数介绍

{
  "name"             : "node-app", //启动app名称
  "cwd"              : "/srv/node-app/current", 
  "args"             : ["--toto=heya coco", "-d", "1"],
  "script"           : "bin/app.js",
  "node_args"        : ["--harmony", " --max-stack-size=102400000"],
  "log_date_format"  : "YYYY-MM-DD HH:mm Z",
  "error_file"       : "/var/log/node-app/node-app.stderr.log",
  "out_file"         : "log/node-app.stdout.log",
  "pid_file"         : "pids/node-geo-api.pid",
  "instances"        : 6, //or 0 => 'max'
  "min_uptime"       : "200s", // 200 seconds, defaults to 1000
  "max_restarts"     : 10, // defaults to 15
  "max_memory_restart": "1M", // 1 megabytes, e.g.: "2G", "10M", "100K", 1024 the default unit is byte.
  "cron_restart"     : "1 0 * * *",
  "watch"            : false,
  "ignore_watch"      : ["[\\/\\\\]\\./", "node_modules"],
  "merge_logs"       : true,
  "exec_interpreter" : "node",
  "exec_mode"        : "fork",
  "autorestart"      : false, // enable/disable automatic restart when an app crashes or exits
  "vizion"           : false, // enable/disable vizion features (versioning control)
  // Default environment variables that will be injected in any environment and at any start
  "env": {
    "NODE_ENV": "production",
    "AWESOME_SERVICE_API_TOKEN": "xxx"
  }
  "env_*" : {
    "SPECIFIC_ENV" : true
  }
}

配置启动环境

启动指定的环境:pm2 start --env test
pm2 start --env prod

PM2 命令

    start [options] [name|namespace|file|ecosystem|id...]        start and daemonize an app
    trigger <id|proc_name|namespace|all> <action_name> [params]  trigger process action
    deploy <file|environment>                                    deploy your json
    startOrRestart <json>                                        start or restart JSON file
    startOrReload <json>                                         start or gracefully reload JSON file
    pid [app_name]                                               return pid of [app_name] or all
    create                                                       return pid of [app_name] or all
    startOrGracefulReload <json>                                 start or gracefully reload JSON file
    stop [options] <id|name|namespace|all|json|stdin...>         stop a process
    restart [options] <id|name|namespace|all|json|stdin...>      restart a process
    scale <app_name> <number>                                    scale up/down a process in cluster mode depending on total_number param
    profile:mem [time]                                           Sample PM2 heap memory
    profile:cpu [time]                                           Profile PM2 cpu
    reload <id|name|namespace|all>                               reload processes (note that its for app using HTTP/HTTPS)
    id <name>                                                    get process id by name
    inspect <name>                                               inspect a process
    delete|del <name|id|namespace|script|all|json|stdin...>      stop and delete a process from pm2 process list
    sendSignal <signal> <pm2_id|name>                            send a system signal to the target process
    ping                                                         ping pm2 daemon - if not up it will launch it
    updatePM2                                                    update in-memory PM2 with local PM2
    update                                                       (alias) update in-memory PM2 with local PM2
    install|module:install [options] <module|git:/>              install or update a module and run it forever
    module:update <module|git:/>                                 update a module and run it forever
    module:generate [app_name]                                   Generate a sample module in current folder
    uninstall|module:uninstall <module>                          stop and uninstall a module
    package [target]                                             Check & Package TAR type module
    publish|module:publish [options] [folder]                    Publish the module you are currently on
    set [key] [value]                                            sets the specified config <key> <value>
    multiset <value>                                             multiset eg "key1 val1 key2 val2
    get [key]                                                    get value for <key>
    conf [key] [value]                                           get / set module config values
    config <key> [value]                                         get / set module config values
    unset <key>                                                  clears the specified config <key>
    report                                                       give a full pm2 report for https://github.com/Unitech/pm2/issues
    link [options] [secret] [public] [name]                      link with the pm2 monitoring dashboard
    unlink                                                       unlink with the pm2 monitoring dashboard
    monitor [name]                                               monitor target process
    unmonitor [name]                                             unmonitor target process
    open                                                         open the pm2 monitoring dashboard
    plus|register [options] [command] [option]                   enable pm2 plus
    login                                                        Login to pm2 plus
    logout                                                       Logout from pm2 plus
    dump|save [options]                                          dump all processes for resurrecting them later
    cleardump                                                    Create empty dump file
    send <pm_id> <line>                                          send stdin to <pm_id>
    attach <pm_id> [comman]                                      attach stdin/stdout to application identified by <pm_id>
    resurrect                                                    resurrect previously dumped processes
    unstartup [platform]                                         disable the pm2 startup hook
    startup [platform]                                           enable the pm2 startup hook
    logrotate                                                    copy default logrotate configuration
    ecosystem|init [mode]                                        generate a process conf file. (mode = null or simple)
    reset <name|id|all>                                          reset counters for process
    describe <name|id>                                           describe all parameters of a process
    desc <name|id>                                               (alias) describe all parameters of a process
    info <name|id>                                               (alias) describe all parameters of a process
    show <name|id>                                               (alias) describe all parameters of a process
    env <id>                                                     list all environment variables of a process id
    list|ls                                                      list all processes
    l                                                            (alias) list all processes
    ps                                                           (alias) list all processes
    status                                                       (alias) list all processes
    jlist                                                        list all processes in JSON format
    sysmonit                                                     start system monitoring daemon
    slist|sysinfos [options]                                     list system infos in JSON
    prettylist                                                   print json in a prettified JSON
    monit                                                        launch termcaps monitoring
    imonit                                                       launch legacy termcaps monitoring
    dashboard|dash                                               launch dashboard with monitoring and logs
    flush [api]                                                  flush logs
    reloadLogs                                                   reload all logs
    logs [options] [id|name|namespace]                           stream logs file. Default stream all logs
    kill                                                         kill daemon
    pull <name> [commit_id]                                      updates repository for a given app
    forward <name>                                               updates repository to the next commit for a given app
    backward <name>                                              downgrades repository to the previous commit for a given app
    deepUpdate                                                   performs a deep update of PM2
    serve|expose [options] [path] [port]                         serve a directory over http via port
    autoinstall
    examples                                                     display pm2 usage examples
```
 类似资料: