通常我们会使用ssh工具连接远程的云主机部署运行web应用,但是当我们断开ssh回话后,应用也随之断开,那么有什么方法能使得应用不受ssh断开的影响呢?当然有方法!
一种方法大家可以参考《Linux下使Shell 命令脱离终端在后台运行》。
今天我们主要来介绍怎么使用forever管理node应用。
安装
npm install -g forever
使用
- 启动应用
forever start app.js
- 停止应用
forever stop app.js
更多参数
$ forever --help usage: forever [action] [options] SCRIPT [script-options] Monitors the script specified in the current process or as a daemon actions: start Start SCRIPT as a daemon stop Stop the daemon SCRIPT by Id|Uid|Pid|Index|Script stopall Stop all running forever scripts restart Restart the daemon SCRIPT restartall Restart all running forever scripts list List all running forever scripts config Lists all forever user configuration set <key> <val> Sets the specified forever config <key> clear <key> Clears the specified forever config <key> logs Lists log files for all forever processes logs <script|index> Tails the logs for <script|index> columns add <col> Adds the specified column to the output in `forever list` columns rm <col> Removed the specified column from the output in `forever list` columns set <cols> Set all columns for the output in `forever list` cleanlogs [CAREFUL] Deletes all historical forever log files options: -m MAX Only run the specified script MAX times -l LOGFILE Logs the forever output to LOGFILE -o OUTFILE Logs stdout from child script to OUTFILE -e ERRFILE Logs stderr from child script to ERRFILE -p PATH Base path for all forever related files (pid files, etc.) -c COMMAND COMMAND to execute (defaults to node) -a, --append Append logs -f, --fifo Stream logs to stdout -n, --number Number of log lines to print --pidFile The pid file --uid Process uid, useful as a namespace for processes (must wrap in a string) e.g. forever start --uid "production" app.js forever stop production --sourceDir The source directory for which SCRIPT is relative to --workingDir The working directory in which SCRIPT will execute --minUptime Minimum uptime (millis) for a script to not be considered "spinning" --spinSleepTime Time to wait (millis) between launches of a spinning script. --colors --no-colors will disable output coloring --plain Disable command line colors -d, --debug Forces forever to log debug output -v, --verbose Turns on the verbose messages from Forever -s, --silent Run the child script silencing stdout and stderr -w, --watch Watch for file changes --watchDirectory Top-level directory to watch from --watchIgnore To ignore pattern when watch is enabled (multiple option is allowed) -t, --killTree Kills the entire child process tree on `stop` --killSignal Support exit signal customization (default is SIGKILL), used for restarting script gracefully e.g. --killSignal=SIGTERM -h, --help You're staring at it [Long Running Process] The forever process will continue to run outputting log messages to the console. ex. forever -o out.log -e err.log my-script.js [Daemon] The forever process will run as a daemon which will make the target process start in the background. This is extremely useful for remote starting simple node.js scripts without using nohup. It is recommended to run start with -o -l, & -e. ex. forever start -l forever.log -o out.log -e err.log my-daemon.js forever stop my-daemon.js
作者:Devid
链接:http://www.jianshu.com/p/82a64aee0710
经常逛cnode社区,所以早已耳闻pm2,今天我们就研究一下它到底怎么部署项目。
简单教程
首先需要安装pm2:
npm install -g pm2
运行:
pm2 start app.js
初次安装并运行,会有一个高大上的界面:
直接我们介绍过forever,那么pm2与forever相比较有哪些高大上的功能呢?我们看一下对比表格:
Feature | Forever | PM2 |
---|---|---|
Keep Alive | ✔ | ✔ |
Coffeescript | ✔ | |
Log aggregation | ✔ | |
API | ✔ | |
Terminal monitoring | ✔ | |
Clustering | ✔ | |
JSON configuration | ✔ |
我们可以很直观的看出,pm2相比较Forever,功能更加强大一些。
查看运行状态
我们可以通过简单的命令查看应用的运行状态:
pm2 list
效果如下:
ANodeBlog应用正在运行,pid为31480,并且占用内存为89.113 MB。
追踪资源运行情况
pm2 monit
会看到应用资源的实时运行情况
查看应用详细部署状态
如果我们想要查看一个应用详细的运行状态,比如ANodeBlog
的状态,可以运行:
pm2 describe 3
"3"是指App Id。
结果如下:
查看日志
pm2 logs
系统会打印出详细的logs。
重启应用
pm2 restart appId
停止应用
想要终止应用,只需要运行:
pm2 stop app.js
强健的API
在项目中运行:
pm2 web
然后浏览器访问http://localhost:9615 你会有惊喜!
预定义运行配置文件
我们可以预定义一个配置文件,然后制定运行这个配置文件,比如我们定义一个文件process.json
,内容如下:
{ "apps": [ { "name": "ANodeBlog", "script": "bin/www", "watch": "../", "log_date_format": "YYYY-MM-DD HH:mm Z" } ] }
然后可以通过
pm2 start process.json
运行这个App。
总结
常用命令总结如下:
- 安装pm2
npm install -g pm2
- 启动应用
pm2 start app.js
- 列出所有应用
pm2 list
- 查看资源消耗
pm2 monit
- 查看某一个应用状态
pm2 describe [app id]
- 查看所有日志
pm2 logs
- 重启应用
pm2 restart [app id]
- 停止应用
pm2 stop [app id]
- 开启api访问
pm2 web
更多pm2内容请参考官方文档:http://pm2.keymetrics.io/docs/usage/quick-start
作者:Devid
链接:http://www.jianshu.com/p/fdc12d82b661
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
沒有留言:
張貼留言