# PM2

# 常用命令

1. pm2 start <file.js> 启动进程

2. pm2 stop [<name> | <pid>] 停止进程

3. pm2 restart & reload [<name> | <pid>] 重启进程

4. pm2 delete [<name> | <pid>] 删除进程

4. pm2 ls 列出所有进程

┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 10 │ my-study-notes     │ fork     │ 0    │ online    │ 0%       │ 52.5mb   │
│ 9  │ nuxt-juejin-proje… │ fork     │ 0    │ online    │ 0.2%     │ 92.6mb   │
│ 2  │ vue-bytedanceJob   │ fork     │ 2    │ online    │ 0.2%     │ 57.4mb   │
│ 1  │ vue-music          │ fork     │ 2    │ online    │ 0%       │ 62.5mb   │
│ 0  │ vue-sell           │ fork     │ 2    │ online    │ 0.2%     │ 52.9mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 全部命令

 Usage: pm2 [cmd] app

  Options:

    -V, --version                                                output the version number
    -v --version                                                 print pm2 version
    -s --silent                                                  hide all messages
    --ext <extensions>                                           watch only this file extensions
    -n --name <name>                                             set a name for the process in the process list
    -m --mini-list                                               display a compacted list without formatting
    --interpreter <interpreter>                                  set a specific interpreter to use for executing app, default: node
    --interpreter-args <arguments>                               set arguments to pass to the interpreter (alias of --node-args)
    --node-args <node_args>                                      space delimited arguments to pass to node
    -o --output <path>                                           specify log file for stdout
    -e --error <path>                                            specify log file for stderr
    -l --log [path]                                              specify log file which gathers both stdout and stderr
    --filter-env [envs]                                          filter out outgoing global values that contain provided strings (default: )
    --log-type <type>                                            specify log output style (raw by default, json optional)
    --log-date-format <date format>                              add custom prefix timestamp to logs
    --time                                                       enable time logging
    --disable-logs                                               disable all logs storage
    --env <environment_name>                                     specify which set of environment variables from ecosystem file must be injected
    -a --update-env                                              force an update of the environment with restart/reload (-a <=> apply)
    -f --force                                                   force actions
    -i --instances <number>                                      launch [number] instances (for networked app)(load balanced)
    --parallel <number>                                          number of parallel actions (for restart/reload)
    --shutdown-with-message                                      shutdown an application with process.send('shutdown') instead of process.kill(pid, SIGINT)
    -p --pid <pid>                                               specify pid file
    -k --kill-timeout <delay>                                    delay before sending final SIGKILL signal to process
    --listen-timeout <delay>                                     listen timeout on application reload
    --max-memory-restart <memory>                                Restart the app if an amount of memory is exceeded (in bytes)
    --restart-delay <delay>                                      specify a delay between restarts (in milliseconds)
    --exp-backoff-restart-delay <delay>                          specify a delay between restarts (in milliseconds)
    -x --execute-command                                         execute a program using fork system
    --max-restarts [count]                                       only restart the script COUNT times
    -u --user <username>                                         define user when generating startup script
    --uid <uid>                                                  run target script with <uid> rights
    --gid <gid>                                                  run target script with <gid> rights
    --namespace <ns>                                             start application within specified namespace
    --cwd <path>                                                 run target script from path <cwd>
    --hp <home path>                                             define home path when generating startup script
    --wait-ip                                                    override systemd script to wait for full internet connectivity to launch pm2
    --service-name <name>                                        define service name when generating startup script
    -c --cron <cron_pattern>                                     restart a running process based on a cron pattern
    -w --write                                                   write configuration in local folder
    --no-daemon                                                  run pm2 daemon in the foreground if it doesn't exist already
    --source-map-support                                         force source map support
    --only <application-name>                                    with json declaration, allow to only act on one application
    --disable-source-map-support                                 force source map support
    --wait-ready                                                 ask pm2 to wait for ready event from your app
    --merge-logs                                                 merge logs from different instances but keep error and out separated
    --watch [paths]                                              watch application folder for changes (default: )
    --ignore-watch <folders|files>                               List of paths to ignore (name or regex)
    --watch-delay <delay>                                        specify a restart delay after changing files (--watch-delay 4 (in sec) or 4000ms)
    --no-color                                                   skip colors
    --no-vizion                                                  start an app without vizion feature (versioning control)
    --no-autorestart                                             start an app without automatic restart
    --no-treekill                                                Only kill the main process, not detached children
    --no-pmx                                                     start an app without pmx
    --no-automation                                              start an app without pmx
    --trace                                                      enable transaction tracing with km
    --disable-trace                                              disable transaction tracing with km
    --attach                                                     attach logging after your start/restart/stop/reload
    --v8                                                         enable v8 data collecting
    --event-loop-inspector                                       enable event-loop-inspector dump in pmx
    --deep-monitoring                                            enable all monitoring tools (equivalent to --v8 --event-loop-inspector --trace)
    -h, --help                                                   output usage information

  Commands:

    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
    *
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150