`
fantaxy025025
  • 浏览: 1248014 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

nodejs之supervisor__supervisor的python版本

 
阅读更多

 

nodejs之supervisor

  如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你 在开发 Node.js 实现的 HTTP 应用时会发现,无论你修改了代码的哪一部份,都必须终止 Node.js再重新运行才会奏效。这是因为 Node.js 只有在第一次引用到某部份时才会去解析脚 本文件,以后都会直接访问内存,避免重复载入,而 PHP 则总是重新读取并解析脚本(如 果没有专门的优化配置)。Node.js的这种设计虽然有利于提高性能,却不利于开发调试,因 为我们在开发过程中总是希望修改后立即看到效果,而不是每次都要终止进程并重启。

supervisor 可以帮助你实现这个功能,它会监视你对代码的改动,并自动重启 Node.js。 使用方法很简单,首先使用 npm 安装 supervisor:

    $ npm install -g supervisor

如果你使用的是 Linux 或 Mac,直接键入上面的命令很可能会有权限错误。原因是 npm 需要把 supervisor 安装到系统目录,需要管理员授权,可以使用 sudo npm install -g supervisor 命令来安装。

接下来,使用 supervisor 命令启动 app.js: $ supervisor app.js

    DEBUG: Running node-supervisor with
    DEBUG:   program 'app.js'
    DEBUG:   --watch '.'
    DEBUG:   --extensions 'node|js'
    DEBUG:   --exec 'node'

DEBUG: Starting child process with 'node app.js'
DEBUG: Watching directory '/home/byvoid/.' for changes. HTTP server is listening at port 3000.

  当代码被改动时,运行的脚本会被终止,然后重新启动。在终端中显示的结果如下:
    DEBUG: crashing child
    DEBUG: Starting child process with 'node app.js'
    HTTP server is listening at port 3000.

supervisor 这个小工具可以解决开发中的调试问题。 

 

【python之supervisor

Supervisor是一个进程管理工具,官方的说法

用途就是有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断。当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了Supervisor

这个工具主要就两个命令:

supervisord : supervisor的服务器端部分,启动supervisor就是运行这个命令

supervisorctl:启动supervisor的命令行窗口。

安装(Centos):

  1. # yum install python-setuptools
  2. # easy_install supervisor
  3. 如果easy_install不好使就从官方下载:
  4. 然后通过python安装:
  5. # tar zxf supervisor-3.1.3.tar.gz
  6. # cd supervisor
  7. # python setup.py install

成功安装后可以登陆python控制台输入import supervisor 查看是否能成功加载。

生成配置文件(supervisord.conf):

echo_supervisord_conf > /etc/supervisord.conf

修改配置文件:

在supervisord.conf最后增加(分号后边的表示注释,可以不写):

  1. [program:bandwidth]
  2. command=python26 /usr/local/bin/bandwidth.sh  ;需要执行的命令wd)
  3. user =root  ;(default  is  current  user , required  if  root)
  4. autostart=true  ;start at supervisord start (default: true)
  5. autorestart=true  ;whether/when to restart (default: unexpected)
  6. startsecs=3  ;number of secs prog must stay running ( def . 1)
  7. stderr_logfile=/tmp/bandwidth_err.log  ;redirect proc stderr to stdout (default false) 错误输出重定向
  8. stdout_logfile=/tmp/bandwidth.log  ;stdout log path, NONE  for none; default AUTO, log输出
  9. (更多配置说明请参考:http://supervisord.org/configuration.html)

运行命令:

supervisord -c /etc/supervisord.conf  //启动supervisor

supervisorctl //打开命令行

  1. [root @iZ2365j7l5bZ  bin]# supervisorctl status   
  2. bandwidth                        RUNNING   pid  2423 , uptime  0 : 06 : 35   
  3. [root @iZ2365j7l5bZ  bin]# supervisorctl help   
  4.   
  5. default  commands (type help <topic>):   
  6. =====================================   
  7. add    clear  fg        open  quit    remove  restart   start   stop  update    
  8. avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version  

ctl中: help //查看命令

ctl中: status //查看状态

另外有一个坑需要注意:如果修改了 /etc/supervisord.conf ,需要执行 supervisorctl reload 来重新加载配置文件,否则不会生效。。。

 

 

+

+

+

=

+

=

+

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics