Linux后台进程管理利器:supervisor

Linux的后台进程运行有好几种方法,例如nohup,screen等,可是,若是是一个服务程序,要可靠地在后台运行,咱们就须要把它作成daemon,最好还能监控进程状态,在乎外结束时能自动重启。sql

supervisor就是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。shell

安装supervisor

Debian / Ubuntu能够直接经过apt安装:ruby

# apt-get install supervisor 

而后,给咱们本身开发的应用程序编写一个配置文件,让supervisor来管理它。每一个进程的配置文件均可以单独分拆,放在/etc/supervisor/conf.d/目录下,以.conf做为扩展名,例如,app.conf定义了一个gunicorn的进程:app

[program:app] command=/usr/bin/gunicorn -w 1 wsgiapp:application directory=/srv/www user=www-data 

其中,进程app定义在[program:app]中,command是命令,directory是进程的当前目录,user是进程运行的用户身份。ui

重启supervisor,让配置文件生效,而后运行命令supervisorctl启动进程:spa

# supervisorctl start app 

中止进程:命令行

# supervisorctl stop app 

若是要在命令行中使用变量,就须要本身先编写一个shell脚本:code

#!/bin/sh /usr/bin/gunicorn -w `grep -c ^processor /proc/cpuinfo` wsgiapp:application 

而后,加上x权限,再把command指向该shell脚本便可。进程

相关文章
相关标签/搜索