美文网首页
使用 pm2 管理 node等 服务

使用 pm2 管理 node等 服务

作者: ag4kd | 来源:发表于2020-07-23 10:57 被阅读0次

    安装pm2

    官网

    PM2 is a process manager. It manages your applications states, so you can start, stop, restart and delete processes.

    pm2需要全局安装

    cnpm install pm2@latest -g
    

    使用

    进入项目根目录

    Start an app

    pm2 start app.js
    

    Or start any other application easily:

    pm2 start bashscript.sh
    pm2 start python-app.py --watch
    pm2 start binary-file -- --port 1520
    

    As you can see many options are available to manage your application with PM2. You will discover them depending on your use case.

    • cli 参数
    • --name <app_name>:Specify an app name
    • --watch:Watch and Restart app when files change
    • --max-memory-restart <200MB>:Set memory threshold for app reload
    • --log <log_path>:Specify log file
    • -- arg1 arg2 arg3:Pass extra arguments to the script
    • --restart-delay <delay in ms>:Delay between automatic restarts
    • --time:Prefix logs with time
    • --no-autorestart:Do not auto restart app
    • --cron <cron_pattern>:Specify cron for forced restart
    • --no-daemon:Attach to application log

    Managing processes

    Managing application state is simple here are the commands

    pm2 restart app_name
    pm2 reload app_name
    pm2 stop app_name
    pm2 delete app_name
    

    Instead of app_name you can pass:

    • all to act on all processes
    • id to act on a specific process id

    Check status, logs, metrics

    List managed applications

    List the status of all application managed by PM2

    pm2 [list|ls|status]
    

    Display logs

    To display logs in realtime:

    pm2 logs
    

    To dig in older logs:

    pm2 logs --lines 200
    

    Terminal Based Dashboard

    Here is a realtime dashboard that fits directly into your terminal:

    pm2 monit
    

    pm2.io: Monitoring & Diagnostic Web Interface

    pm2 plus
    

    Updating PM2

    pm2 update
    

    CheatSheet

    Here are some commands that are worth knowing. Just try them with a sample application or with your current web application on your development machine:

    # Fork mode
    pm2 start app.js --name my-api # Name process
    
    # Cluster mode
    pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
    pm2 start app.js -i max      # Same as above, but deprecated.
    pm2 scale app +3             # Scales `app` up by 3 workers
    pm2 scale app 2              # Scales `app` up or down to 2 workers total
    
    # Listing
    
    pm2 list               # Display all processes status
    pm2 jlist              # Print process list in raw JSON
    pm2 prettylist         # Print process list in beautified JSON
    
    pm2 describe 0         # Display all informations about a specific process
    
    pm2 monit              # Monitor all processes
    
    # Logs
    
    pm2 logs [--raw]       # Display all processes logs in streaming
    pm2 flush              # Empty all log files
    pm2 reloadLogs         # Reload all logs
    
    # Actions
    
    pm2 stop all           # Stop all processes
    pm2 restart all        # Restart all processes
    
    pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)
    
    pm2 stop 0             # Stop specific process id
    pm2 restart 0          # Restart specific process id
    
    pm2 delete 0           # Will remove process from pm2 list
    pm2 delete all         # Will remove all processes from pm2 list
    
    # Misc
    
    pm2 reset <process>    # Reset meta data (restarted time...)
    pm2 updatePM2          # Update in memory pm2
    pm2 ping               # Ensure pm2 daemon has been launched
    pm2 sendSignal SIGUSR2 my-app # Send system signal to script
    pm2 start app.js --no-daemon
    pm2 start app.js --no-vizion
    pm2 start app.js --no-autorestart
    

    相关文章

      网友评论

          本文标题:使用 pm2 管理 node等 服务

          本文链接:https://www.haomeiwen.com/subject/bjqpihtx.html