在前面的笔记中,我们启动express项目都是使用命令“npm start”,如果是后台进程,需要借助nohup。
nohup npm start &
然后,怎么查看是否启动成功?
只能借助jobs或ps:
lee@mypc ~/works/nodejs/study21/log-morgan $ jobs
[1]+ Running nohup npm start &
lee@mypc ~/works/nodejs/study21/log-morgan $ ps -aux | grep 'npm'
lee 4062 0.0 0.0 13424 2436 pts/5 S+ 10:15 0:00 grep --colour=auto npm
lee 30413 0.0 0.6 1069532 48752 pts/5 Sl Jan18 0:00 npm
怎么关闭进程?
只能借助kill:
lee@mypc ~/works/nodejs/study21/log-morgan $ kill 30413
lee@mypc ~/works/nodejs/study21/log-morgan $ ps -aux | grep 'npm'
lee 4180 0.0 0.0 13424 2444 pts/5 S+ 10:18 0:00 grep --colour=auto npm
太麻烦了吧,有没有?!
其实,还可以这样启动:
pm2 start app.js
这样查看:
pm2 list
┌──────────┬────┬──────┬───────┬─────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────┼────┼──────┼───────┼─────────┼─────────┼────────┼─────────────┼──────────┤
│ demo1 │ 0 │ fork │ 0 │ errored │ 28 │ 0 │ 0 B │ disabled │
│ app │ 1 │ fork │ 0 │ stopped │ 0 │ 0 │ 0 B │ disabled │
│ app │ 2 │ fork │ 23174 │ online │ 0 │ 18h │ 36.695 MB │ disabled │
│ app │ 3 │ fork │ 0 │ stopped │ 0 │ 0 │ 0 B │ disabled │
└──────────┴────┴──────┴───────┴─────────┴─────────┴────────┴─────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
这样关闭:
pm2 stop 2
这样重启:
pm2 restart 2
简单,方便,使用,有没有?!
pm2是一个带有负载均衡功能的nodejs应用的进程管理产品,功能强大。它可以让应用保持后台运行,重启0等待。
pm2的安装和应用非常简单。
主要命令使用demo如下:
$ npm install pm2 -g # Install PM2
$ pm2 start app.js # Start, Daemonize and auto restart application
$ pm2 start app.js -i 4 # Start 4 instances of application in cluster mode
# it will load balance network queries to each app
$ pm2 start app.js --name="api" # Start application and name it "api"
$ pm2 start app.js --watch # Restart application on file change
$ pm2 start script.sh # Start bash script
$ pm2 list # List all processes started with PM2
$ pm2 monit # Display memory and cpu usage of each app
$ pm2 show [app-name] # Show all informations about application
$ pm2 logs # Display logs of all apps
$ pm2 logs [app-name] # Display logs for a specific app
$ pm2 flush
$ pm2 stop all # Stop all apps
$ pm2 stop 0 # Stop process with id 0
$ pm2 restart all # Restart all apps
$ pm2 reload all # Reload all apps in cluster mode
$ pm2 gracefulReload all # Graceful reload all apps in cluster mode
$ pm2 delete all # Kill and delete all apps
$ pm2 delete 0 # Delete app with id 0
$ pm2 scale api 10 # Scale app with name api to 10 instances
$ pm2 reset [app-name] # Reset number of restart for [app-name]
$ pm2 startup # Generate a startup script to respawn PM2 on boot
$ pm2 save # Save current process list
$ pm2 resurrect # Restore previously save processes
$ pm2 update # Save processes, kill PM2 and restore processes
$ pm2 generate # Generate a sample json configuration file
$ pm2 deploy app.json prod setup # Setup "prod" remote server
$ pm2 deploy app.json prod # Update "prod" remote server
$ pm2 deploy app.json prod revert 2 # Revert "prod" remove server by 2
$ pm2 module:generate [name] # Generate sample module with name [name]
$ pm2 install pm2-logrotate # Install module (here a log rotation system)
$ pm2 uninstall pm2-logrotate # Uninstall module
$ pm2 publish # Increment version, git push and npm publish
时间: 2024-11-10 08:17:10