解决Linux关闭终端(关闭SSH等)后node 网站打不开。

2017-06-07  本文已影响159人  化城

问题:当SSH远程连接到服务器上,然后运行 node server.js 我们在浏览器打开localhost:3000,网站正常访问。但是,关闭SSH,我们的网站打不开了。

解决方法:使用nohup命令让程序在关闭窗口(切换SSH连接)的时候程序还能继续在后台运行。
用node不推荐

推荐方法

用node pm2模块

pm2官方文档:http://pm2.keymetrics.io/docs/usage/quick-start/
github pm2文档: https://github.com/Unitech/pm2

安装pm2 (建议全局安装)

$ npm install pm2 -g

开始你的服务

$ pm2 start server.js

更新pm2

$ pm2 update

所以命令

# General
$ npm install pm2 -g            # Install PM2
$ pm2 start app.js              # Start, Daemonize and auto-restart application (Node)
$ pm2 start app.py              # Start, Daemonize and auto-restart application (Python)
$ pm2 start npm -- start        # Start, Daemonize and auto-restart Node application

# Cluster Mode (Node.js only)
$ pm2 start app.js -i 4         # Start 4 instances of application in cluster mode
                                # it will load balance network queries to each app
$ pm2 reload all                # Zero Second Downtime Reload
$ pm2 scale [app-name] 10       # Scale Cluster app to 10 process

# Process Monitoring
$ 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

# Log management
$ pm2 logs                      # Display logs of all apps
$ pm2 logs [app-name]           # Display logs for a specific app
$ pm2 logs --json               # Logs in JSON format
$ pm2 flush
$ pm2 reloadLogs

# Process State Management
$ pm2 start app.js --name="api" # Start application and name it "api"
$ pm2 start app.js -- -a 34     # Start app and pass option "-a 34" as argument
$ pm2 start app.js --watch      # Restart application on file change
$ pm2 start script.sh           # Start bash script
$ pm2 start app.json            # Start all applications declared in app.json
$ pm2 reset [app-name]          # Reset all counters
$ pm2 stop all                  # Stop all apps
$ pm2 stop 0                    # Stop process with id 0
$ pm2 restart all               # Restart all apps
$ 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

# Startup/Boot management
$ pm2 startup                   # Detect init system, generate and configure pm2 boot on startup
$ pm2 save                      # Save current process list
$ pm2 resurrect                 # Restore previously save processes
$ pm2 unstartup                 # Disable and remove startup system

$ pm2 update                    # Save processes, kill PM2 and restore processes
$ pm2 generate                  # Generate a sample json configuration file

# Deployment
$ 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" remote server by 2

# Module system
$ 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

pm2 ls

感觉功能太强大了,更多知识 查看github

有时候我们可能不直接运行js 脚本 而是 运行 npm -- start ,下面是解决方案


{
    "name": "my_app",
    "scripts": [
        "start": "node main.js"
    ]
}
//直接运行下面代码就可以调用 package.json 里面的scripts 里面的start 命令
$ pm2 start npm -- start

在此做一下记录,如有问题可以留言。

上一篇下一篇

猜你喜欢

热点阅读