xgqfrms / FEIQA

FEIQA: Front End Interviews Question & Answers
https://feiqa.xgqfrms.xyz
MIT License
7 stars 0 forks source link

PM2 #37

Open xgqfrms opened 6 years ago

xgqfrms commented 6 years ago

PM2

https://www.npmjs.com/package/pm2

https://github.com/Unitech/pm2

docs

https://pm2.io/doc/en/runtime/quick-start/

https://pm2.io/doc/en/runtime/overview/

https://pm2.io/doc/en/runtime/guide/installation/


old docs

https://pm2.keymetrics.io/docs/usage/quick-start/ https://pm2.keymetrics.io/docs/usage/quick-start/#options

https://pm2.keymetrics.io/docs/usage/pm2-api/

https://pm2.keymetrics.io/docs/usage/watch-and-restart/#auto-restart-apps-on-file-change

https://pm2.keymetrics.io/docs/usage/startup/

https://id.keymetrics.io/api/oauth/register

https://github.com/pm2-hive/pm2-hive.github.io/tree/master/docs/features/startup.md



# install

$ npm install pm2 -g

image

image

$ pm2 list

$ mp2 start server.js

image

xgqfrms commented 6 years ago

https://pm2online.com/

https://devo.ps/blog/goodbye-node-forever-hello-pm2/

https://futurestud.io/tutorials/pm2-restart-processes-after-system-reboot

https://stackoverflow.com/questions/21506505/how-to-use-pm2-startup-command-on-debian-platform

https://github.com/Unitech/pm2#startup-script-generation--pm2-startup

xgqfrms commented 6 years ago

bug

image

db.sh


#!/bin/sh

# echo "^-v-^ JSON DB is running in development env!" && npm run db

# echo "^-v-^ JSON DB is running in development env!" && nodemon -w ./server.js localhost 8888

JSONDB="nodemon -w ./server.js localhost 7777"

${JSONDB} &

# chmod +x db.sh
# sudo ./db.sh
# nodemon -w ./server.js localhost 8888

# /bin/sh db.sh

# ps -ef | grep node

# sudo kill -9 <PID>

# sudo killall -9 <NAME>
# sudo killall -9 node
xgqfrms commented 6 years ago

PM2 Tutorials

docs

https://pm2.xgqfrms.xyz

auto restart

You can then setup a startup script, to automatically start your process list across machine restarts.

然后,您可以设置启动脚本,以便在机器重新启动时自动启动进程列表。

https://pm2.io/doc/en/runtime/quick-start/#routine

https://pm2.io/doc/en/runtime/guide/startup-hook/


pm2 startup
$ [PM2] You have to run this command as root. Execute the following command:
$ sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
xgqfrms commented 6 years ago

image


[root@DataServerTest2 api]# pm2 startup
[PM2] Init System found: systemv
Platform systemv
Template
#!/bin/bash
### BEGIN INIT INFO
# Provides:        pm2
# Required-Start:  $local_fs $remote_fs $network
# Required-Stop:   $local_fs $remote_fs $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: PM2 Init script
# Description: PM2 process manager
### END INIT INFO

NAME=pm2
PM2=/usr/lib/node_modules/pm2/bin/pm2
USER=root
DEFAULT=/etc/default/$NAME

export PATH=/usr/bin:$PATH
export PM2_HOME="/root/.pm2"

# The following variables can be overwritten in $DEFAULT

# maximum number of open files
MAX_OPEN_FILES=

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
    . "$DEFAULT"
fi

# set maximum open files if set
if [ -n "$MAX_OPEN_FILES" ]; then
    ulimit -n $MAX_OPEN_FILES
fi

get_user_shell() {
    local shell
    shell=$(getent passwd "${1:-$(whoami)}" | cut -d: -f7 | sed -e 's/[[:space:]]*$//')

    if [[ $shell == *"/sbin/nologin" ]] || [[ $shell == "/bin/false" ]] || [[ -z "$shell" ]];
    then
      shell="/bin/bash"
    fi

    echo "$shell"
}

super() {
    local shell
    shell=$(get_user_shell $USER)
    su - "$USER" -s "$shell" -c "PATH=$PATH; PM2_HOME=$PM2_HOME $*"
}

start() {
    echo "Starting $NAME"
    super $PM2 resurrect
}

stop() {
    super $PM2 kill
}

restart() {
    echo "Restarting $NAME"
    stop
    start
}

reload() {
    echo "Reloading $NAME"
    super $PM2 reload all
}

status() {
    echo "Status for $NAME:"
    super $PM2 list
    RETVAL=$?
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    force-reload)
        reload
        ;;
    *)
        echo "Usage: {start|stop|status|restart|reload|force-reload}"
        exit 1
        ;;
esac
exit $RETVAL

Target path
/etc/init.d/pm2-root
Command list
[ 'chmod +x /etc/init.d/pm2-root',
  'mkdir -p /var/lock/subsys',
  'touch /var/lock/subsys/pm2-root',
  'chkconfig --add pm2-root',
  'chkconfig pm2-root on',
  'initctl list' ]
[PM2] Writing init configuration in /etc/init.d/pm2-root
[PM2] Making script booting at startup...
[PM2] [-] Executing: chmod +x /etc/init.d/pm2-root...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: mkdir -p /var/lock/subsys...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: touch /var/lock/subsys/pm2-root...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: chkconfig --add pm2-root...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: chkconfig pm2-root on...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: initctl list...
rc stop/waiting
tty (/dev/tty3) start/running, process 2384
tty (/dev/tty2) start/running, process 2382
tty (/dev/tty1) start/running, process 2380
tty (/dev/tty6) start/running, process 2390
tty (/dev/tty5) start/running, process 2388
tty (/dev/tty4) start/running, process 2386
plymouth-shutdown stop/waiting
control-alt-delete stop/waiting
rcS-emergency stop/waiting
readahead-collector stop/waiting
kexec-disable stop/waiting
quit-plymouth stop/waiting
rcS stop/waiting
prefdm stop/waiting
init-system-dbus stop/waiting
ck-log-system-restart stop/waiting
readahead stop/waiting
ck-log-system-start stop/waiting
splash-manager stop/waiting
start-ttys stop/waiting
readahead-disable-services stop/waiting
ck-log-system-stop stop/waiting
rcS-sulogin stop/waiting
serial stop/waiting
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemv

sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
xgqfrms commented 6 years ago

sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin pm2 startup <distribution> -u <user> --hp <home-path>

# <distribution>
# systemv

# <user>
# root

# <home-path>
# PM2_HOME ???

sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin pm2 startup systemv -u root --hp <home-path>

save path ???

/root/.pm2/dump.pm2

image

docs

https://github.com/xgqfrms/FEIQA/issues/37#issuecomment-420133940

xgqfrms commented 6 years ago

pm2 -h & pm2 --help


[root@DataServerTest2 api]# pm2 -h

  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
    --log-type <type>                                 specify log output style (raw by default, json optional)
    --log-date-format <date format>                   add custom prefix timestamp to logs
    --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)
    -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)
    -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
    --cwd <path>                                      run target script as <username>
    --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
    --sort <field_name:sort>                          sort process according to field's name
    --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|file|ecosystem|id...]       start and daemonize an app
    trigger <proc_name> <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
    startOrGracefulReload <json>                      start or gracefully reload JSON file
    stop [options] <id|name|all|json|stdin...>        stop a process
    restart [options] <id|name|all|json|stdin...>     restart a process
    scale <app_name> <number>                         scale up/down a process in cluster mode depending on total_number param
    snapshot                                          snapshot PM2 memory
    profile <command>                                 profile CPU
    reload <name|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|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
    publish|module:publish                            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
    enterprise [options] [secret] [public] [name]     enable pm2 enterprise
    plus|register [options] [secret] [public] [name]  enable pm2 plus
    login                                             Login to pm2 plus
    logout                                            Logout from pm2 plus
    web                                               launch a health API on 0.0.0.0:9615
    dump|save                                         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 <id>                                     describe all parameters of a process id
    desc <id>                                         (alias) describe all parameters of a process id
    info <id>                                         (alias) describe all parameters of a process id
    show <id>                                         (alias) describe all parameters 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
    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]                          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
    examples                                          display pm2 usage examples
    *
[root@DataServerTest2 api]# 
xgqfrms commented 6 years ago

linux & version

image

https://pm2.io/doc/en/runtime/guide/startup-hook/#compatibility

user & path

https://pm2.io/doc/en/runtime/guide/startup-hook/#user-permissions

image

xgqfrms commented 6 years ago

SSH

https://pm2.io/doc/en/runtime/guide/easy-deploy-with-ssh/

CLI

https://pm2.io/doc/en/runtime/reference/pm2-cli/

ENV

process.env.ENV_NAME

https://pm2.io/doc/en/runtime/best-practices/environment-variables/


module.exports = {
    apps: [{
        name: "app",
        script: "./app.js",
        env: {
            NODE_ENV: "development"
        },
        env_production: {
            NODE_ENV: "production",
        }
    }]
};
xgqfrms commented 6 years ago

https://blog.linuxeye.cn/435.html

五、自定义启动文件

创建一个test.json的示例文件,格式如下:


{
  "apps":
    {
      "name": "test",
      "cwd": "/data/wwwroot/nodejs",
      "script": "./test.sh",
      "exec_interpreter": "bash",
      "min_uptime": "60s",
      "max_restarts": 30,
      "exec_mode" : "cluster_mode",
      "error_file" : "./test-err.log",
      "out_file": "./test-out.log",
      "pid_file": "./test.pid"
      "watch": false
    }
}

# 添加执行权限
$ chmod +x test.sh      

#启动 
$ pm2 start test.json

options


apps:json结构,apps是一个数组,每一个数组成员就是对应一个pm2中运行的应用

name:应用程序的名称

cwd:应用程序所在的目录

script:应用程序的脚本路径

exec_interpreter:应用程序的脚本类型,这里使用的shell,默认是nodejs

min_uptime:最小运行时间,这里设置的是60s即如果应用程序在60s内退出,pm2会认为程序异常退出,此时触发重启max_restarts设置数量

max_restarts:设置应用程序异常退出重启的次数,默认15次(从0开始计数)

exec_mode:应用程序启动模式,这里设置的是cluster_mode(集群),默认是fork

error_file:自定义应用程序的错误日志文件

out_file:自定义应用程序日志文件

pid_file:自定义应用程序的pid文件

watch:是否启用监控模式,默认是false。如果设置成true,当应用程序变动时,pm2会自动重载。这里也可以设置你要监控的文件。
xgqfrms commented 6 years ago

image

pm2 show

image

watch

image

xgqfrms commented 6 years ago

OK ???


[root@DataServerTest2 ~]# cd /usr/local/apache2/webapps/F10-Components-Framework/componentPlatform/api
[root@DataServerTest2 api]# pwd
/usr/local/apache2/webapps/F10-Components-Framework/componentPlatform/api
[root@DataServerTest2 api]# pm2 l
©°©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©´
©¦ App name ©¦ id ©¦ mode    ©¦ pid ©¦ status  ©¦ restart ©¦ uptime ©¦ cpu ©¦ mem    ©¦ user ©¦ watching ©¦
©À©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©È
©¦ json-db  ©¦ 1  ©¦ cluster ©¦ 0   ©¦ errored ©¦ 174     ©¦ 0      ©¦ 0%  ©¦ 0 B    ©¦ root ©¦ disabled ©¦
©¦ server   ©¦ 0  ©¦ fork    ©¦ 0   ©¦ errored ©¦ 75      ©¦ 0      ©¦ 0%  ©¦ 0 B    ©¦ root ©¦ disabled ©¦
©¸©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¼
 Use `pm2 show <id|name>` to get more details about an app
[root@DataServerTest2 api]# pm2 restart server.js 
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [server.js](ids: 0)
[PM2] [server](0) ?
©°©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©´
©¦ App name ©¦ id ©¦ mode    ©¦ pid   ©¦ status  ©¦ restart ©¦ uptime ©¦ cpu ©¦ mem      ©¦ user ©¦ watching ©¦
©À©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©È
©¦ json-db  ©¦ 1  ©¦ cluster ©¦ 0     ©¦ errored ©¦ 174     ©¦ 0      ©¦ 0%  ©¦ 0 B      ©¦ root ©¦ disabled ©¦
©¦ server   ©¦ 0  ©¦ fork    ©¦ 20798 ©¦ online  ©¦ 75      ©¦ 0s     ©¦ 0%  ©¦ 5.5 MB   ©¦ root ©¦ disabled ©¦
©¸©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¼
 Use `pm2 show <id|name>` to get more details about an app
[root@DataServerTest2 api]# pm2 show 0
 Describing process with id 0 - name server 
©°©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©´
©¦ status            ©¦ online                                                                              ©¦
©¦ name              ©¦ server                                                                              ©¦
©¦ restarts          ©¦ 75                                                                                  ©¦
©¦ uptime            ©¦ 15s                                                                                 ©¦
©¦ script path       ©¦ /usr/local/apache2/webapps/F10-Components-Framework/componentPlatform/api/server.js ©¦
©¦ script args       ©¦ N/A                                                                                 ©¦
©¦ error log path    ©¦ /root/.pm2/logs/server-error.log                                                    ©¦
©¦ out log path      ©¦ /root/.pm2/logs/server-out.log                                                      ©¦
©¦ pid path          ©¦ /root/.pm2/pids/server-0.pid                                                        ©¦
©¦ interpreter       ©¦ node                                                                                ©¦
©¦ interpreter args  ©¦ N/A                                                                                 ©¦
©¦ script id         ©¦ 0                                                                                   ©¦
©¦ exec cwd          ©¦ /usr/local/apache2/webapps/F10-Components-Framework/componentPlatform/api           ©¦
©¦ exec mode         ©¦ fork_mode                                                                           ©¦
©¦ node.js version   ©¦ 8.9.1                                                                               ©¦
©¦ node env          ©¦ N/A                                                                                 ©¦
©¦ watch & reload    ©¦ ?                                                                                   ©¦
©¦ unstable restarts ©¦ 0                                                                                   ©¦
©¦ created at        ©¦ 2018-09-11T05:49:03.579Z                                                            ©¦
©¸©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¼
 Code metrics value 
©°©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©´
©¦ Event Loop Latency ©¦ 0.36ms ©¦
©¦ Active handles     ©¦ 4      ©¦
©¸©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¼
 Add your own code metrics: http://bit.ly/code-metrics
 Use `pm2 logs server [--lines 1000]` to display logs
 Use `pm2 monit` to monitor CPU and Memory usage server
[root@DataServerTest2 api]# pm2 l
©°©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©´
©¦ App name ©¦ id ©¦ mode    ©¦ pid   ©¦ status  ©¦ restart ©¦ uptime ©¦ cpu ©¦ mem       ©¦ user ©¦ watching ©¦
©À©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©à©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©È
©¦ json-db  ©¦ 1  ©¦ cluster ©¦ 0     ©¦ errored ©¦ 174     ©¦ 0      ©¦ 0%  ©¦ 0 B       ©¦ root ©¦ disabled ©¦
©¦ server   ©¦ 0  ©¦ fork    ©¦ 20798 ©¦ online  ©¦ 75      ©¦ 3m     ©¦ 0%  ©¦ 28.6 MB   ©¦ root ©¦ disabled ©¦
©¸©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¼
 Use `pm2 show <id|name>` to get more details about an app
[root@DataServerTest2 api]# 
xgqfrms commented 6 years ago

OK

image

package.json


{
    "name": "express-server",
    "version": "1.1.1",
    "description": "express static web server",
    "main": "server.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "app": "node server.js"
    },
    "keywords": [
        "express",
        "node.js"
    ],
    "author": "xgqfrms",
    "license": "MIT",
    "dependencies": {
        "express": "^4.16.2"
    },
    "devDependencies": {
        "json-server": "^0.14.0"
    }
}

server.js


// server.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();

server.use(middlewares);
server.use(router);
server.listen(7777, () => {
    console.log('JSON Server is running');
});

db-config.json


{
    "apps": {
        "name": "json-db",
        "cwd": "/usr/local/apache2/webapps/F10-Components-Framework/componentPlatform/api",
        "script": "./server.js",
        "exec_interpreter": "node",
        "min_uptime": "60s",
        "max_restarts": 30,
        "exec_mode": "cluster_mode",
        "error_file": "./test-err.log",
        "out_file": "./test-out.log",
        "pid_file": "./test.pid",
        "watch": true
    }
}

start APP


$ pm2 start db-config.json

http://bit.ly/code-metrics


nodemon


#!/bin/sh

# echo "^-v-^ JSON DB is running in development env!" && npm run db

# echo "^-v-^ JSON DB is running in development env!" && nodemon -w ./server.js localhost 8888

JSONDB="nodemon -w ./server.js localhost 7777"

${JSONDB} &
# chmod +x db.sh
# sudo ./db.sh
# nodemon -w ./server.js localhost 8888

# /bin/sh db.sh

# ps -ef | grep node
# sudo kill -9 <PID>
xgqfrms commented 6 years ago

image


[root@DataServerTest2 ~]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2
[root@DataServerTest2 ~]# pm2 startup
[PM2] Init System found: systemv
Platform systemv
Template
#!/bin/bash
### BEGIN INIT INFO
# Provides:        pm2
# Required-Start:  $local_fs $remote_fs $network
# Required-Stop:   $local_fs $remote_fs $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: PM2 Init script
# Description: PM2 process manager
### END INIT INFO

NAME=pm2
PM2=/usr/lib/node_modules/pm2/bin/pm2
USER=root
DEFAULT=/etc/default/$NAME

export PATH=/usr/bin:$PATH
export PM2_HOME="/root/.pm2"

# The following variables can be overwritten in $DEFAULT

# maximum number of open files
MAX_OPEN_FILES=

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
    . "$DEFAULT"
fi

# set maximum open files if set
if [ -n "$MAX_OPEN_FILES" ]; then
    ulimit -n $MAX_OPEN_FILES
fi

get_user_shell() {
    local shell
    shell=$(getent passwd "${1:-$(whoami)}" | cut -d: -f7 | sed -e 's/[[:space:]]*$//')

    if [[ $shell == *"/sbin/nologin" ]] || [[ $shell == "/bin/false" ]] || [[ -z "$shell" ]];
    then
      shell="/bin/bash"
    fi

    echo "$shell"
}

super() {
    local shell
    shell=$(get_user_shell $USER)
    su - "$USER" -s "$shell" -c "PATH=$PATH; PM2_HOME=$PM2_HOME $*"
}

start() {
    echo "Starting $NAME"
    super $PM2 resurrect
}

stop() {
    super $PM2 kill
}

restart() {
    echo "Restarting $NAME"
    stop
    start
}

reload() {
    echo "Reloading $NAME"
    super $PM2 reload all
}

status() {
    echo "Status for $NAME:"
    super $PM2 list
    RETVAL=$?
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    force-reload)
        reload
        ;;
    *)
        echo "Usage: {start|stop|status|restart|reload|force-reload}"
        exit 1
        ;;
esac
exit $RETVAL

Target path
/etc/init.d/pm2-root
Command list
[ 'chmod +x /etc/init.d/pm2-root',
  'mkdir -p /var/lock/subsys',
  'touch /var/lock/subsys/pm2-root',
  'chkconfig --add pm2-root',
  'chkconfig pm2-root on',
  'initctl list' ]
[PM2] Writing init configuration in /etc/init.d/pm2-root
[PM2] Making script booting at startup...
[PM2] [-] Executing: chmod +x /etc/init.d/pm2-root...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: mkdir -p /var/lock/subsys...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: touch /var/lock/subsys/pm2-root...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: chkconfig --add pm2-root...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: chkconfig pm2-root on...
[PM2] [v] Command successfully executed.
[PM2] [-] Executing: initctl list...
rc stop/waiting
tty (/dev/tty3) start/running, process 2384
tty (/dev/tty2) start/running, process 2382
tty (/dev/tty1) start/running, process 2380
tty (/dev/tty6) start/running, process 2390
tty (/dev/tty5) start/running, process 2388
tty (/dev/tty4) start/running, process 2386
plymouth-shutdown stop/waiting
control-alt-delete stop/waiting
rcS-emergency stop/waiting
readahead-collector stop/waiting
kexec-disable stop/waiting
quit-plymouth stop/waiting
rcS stop/waiting
prefdm stop/waiting
init-system-dbus stop/waiting
ck-log-system-restart stop/waiting
readahead stop/waiting
ck-log-system-start stop/waiting
splash-manager stop/waiting
start-ttys stop/waiting
readahead-disable-services stop/waiting
ck-log-system-stop stop/waiting
rcS-sulogin stop/waiting
serial stop/waiting
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemv
[root@DataServerTest2 ~]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2
[root@DataServerTest2 ~]# 
xgqfrms commented 6 years ago

Q

image

A

image

xgqfrms commented 6 years ago

https://blog.linuxeye.cn/435.html https://segmentfault.com/a/1190000002539204

xgqfrms commented 5 years ago

pm2 配置 ecosystem.config.js

https://www.cnblogs.com/xgqfrms/p/9626636.html

https://pm2.keymetrics.io/docs/usage/application-declaration/