<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>supervisor &#8211; 张三太爷</title>
	<atom:link href="https://www.somedoc.net/tag/supervisor/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.somedoc.net</link>
	<description>看前面，黑洞洞</description>
	<lastBuildDate>Fri, 03 Jan 2020 07:26:18 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>

<image>
	<url>https://www.somedoc.net/wp-content/uploads/2016/12/cropped-dandycheung-1-32x32.jpg</url>
	<title>supervisor &#8211; 张三太爷</title>
	<link>https://www.somedoc.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Supervisor 上手</title>
		<link>https://www.somedoc.net/2020/01/03/supervisor-%e4%b8%8a%e6%89%8b/</link>
					<comments>https://www.somedoc.net/2020/01/03/supervisor-%e4%b8%8a%e6%89%8b/#respond</comments>
		
		<dc:creator><![CDATA[张三太爷]]></dc:creator>
		<pubDate>Fri, 03 Jan 2020 07:26:18 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[supervisor]]></category>
		<guid isPermaLink="false">http://www.somedoc.net/?p=4241</guid>

					<description><![CDATA[在服务器上部署的应用，经常需要有， 随系统自动启动的能力；  <a href="https://www.somedoc.net/2020/01/03/supervisor-%e4%b8%8a%e6%89%8b/" class="more-link">[&#8230;]</a>]]></description>
										<content:encoded><![CDATA[<p>在服务器上部署的应用，经常需要有，</p>
<ul>
<li>随系统自动启动的能力；</li>
<li>意外崩溃/退出后自行恢复运行的能力；</li>
<li>便于查看运行状况，以及统一管理的能力。</li>
</ul>
<p>当然还有其他需求，不过事实上上述的前两条最为基本。<a href="http://supervisord.org/">Supervisor</a> 是 Python 开发的一个运行于 *nix 系统下的 C/S 结构的管理服务工具。它拥有不止于上述要求的能力。它的 Client 程序为 <code>supervisorctl</code>，长时间运行于后台的守护程序则是 <code>supervisord</code>，后者甚至内置了一个可以连接的 Web Server 供查看服务状况。</p>
<div><code>superviserd</code> 启动时，可以指定一个配置文件，命令行开关为 <code>-c</code> 加配置文件名。如果没有显示指定，则按下列顺序寻找：</div>
<div>
<pre class="crayon-plain-tag">$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf (Supervisor 3.3.0 起)
../etc/supervisord.conf (相对于可执行文件)
../supervisord.conf (相对于可执行文件)</pre><br />
运行起来以后，可以使用 <code>supervisorctl</code> 来查看/控制，状态查看（<code>status all</code> 其实可省略）：<br />
<pre class="crayon-plain-tag">supervisorctl status all</pre><br />
全部停止：<br />
<pre class="crayon-plain-tag">supervisorctl stop all</pre><br />
全部启动：<br />
<pre class="crayon-plain-tag">supervisorctl start all</pre><br />
Web 相关的设置如下：<br />
<pre class="crayon-plain-tag">[inet_http_server]         ; 缺省是关闭的
port=0.0.0.0:9001          ; 指定监听的 IP 地址和端口，*:port 为所有接口
username=user              ; 缺省为无需用户名
password=123456            ; 缺省为无需密码</pre><br />
设置需改后，要记得重新加载以生效（切记！）：<br />
<pre class="crayon-plain-tag">supervisorctl reload</pre><br />
从最佳实践的角度出发，各个需要监控管理的程序最好各自有自己的专属配置，并将之 <code>include</code> 到主配置文件中，这是 *nix 下的常规操作，不赘述。一个程序的配置文件样例如下（仅供参考，不作详解）：<br />
<pre class="crayon-plain-tag">[program:mindoc]
process_name=mindoc
command=/srv/app/mindoc/mindoc
directory=/srv/app/mindoc
autostart=true
autorestart=true
user=someone
numprocs=1
redirect_stderr=true</pre><br />
自带的 <code>echo_supervisord_conf</code> 命令可以输出非常完整且有详细解释的样例配置文件，可以自行查看。</p>
<p>最后一个问题是，别的程序都被 <code>supervisor</code> 管理了，那它自己呢？显然不能揪着自己的头发把自己拽离地面。鉴于 CentOS 作为服务器过于普遍，以其为示例说明开机启动 Supervisor 服务。</p>
<p>首先，<pre class="crayon-plain-tag">vim /lib/systemd/system/supervisord.service</pre> 来创建 Supervisor 的系统服务，</p><pre class="crayon-plain-tag"># supervisord service for systemd (CentOS 7.0+)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target</pre><p>将服务启用：</p><pre class="crayon-plain-tag">systemctl enable supervisord.service</pre><p>启动：</p><pre class="crayon-plain-tag">systemctl start supervisord.service</pre><p>停止：</p><pre class="crayon-plain-tag">systemctl stop supervisord.service</pre><p>最后，把网上一个小朋友的教训引用过来（虽然 1. 他的叙述很乱，我还略微做了加工；2. 如果按照上文的说明进行配置和操作不应该出现此问题）：</p>
<p>确保通过 supervisor 运行的进程只有一个！是的，切记。比如你的操作不是那么的规范：你先通过 <code>supervisord -c /etc/supervisor/supervisord.conf</code> 将 <pre class="crayon-plain-tag">supervisor</pre> 启动了，几天后忘记了之前已启动，当通过 <code>supervisorctl status</code> 去看运行状态时，会看到提示 <pre class="crayon-plain-tag">unix:///tmp/supervisor.sock no such file</pre>，你就可能会想是不是 <code>supervisord</code> 挂掉了？不管那么多了，先启动吧，于是做了个操作 <pre class="crayon-plain-tag">supervisord</pre>。之后看起来服务都正常了，于是干其他事情去了。可能不久就会收到同事的反馈：数据不正常！你忙了一个小时，各种打日志，各种看代码，最终结果却是：有两个进程在跑同一个执行文件！你会发现一个是当前 supervisor 管理的进程，另一个进程的 ppid 和 supervisor 的 pid 不一样！于是你明白了：supervisord 的操作没有杀死旧进程，而是又启动了一个新进程，两个进程同时运行，结果导致你的数据异常。最终你先将旧进程的父进程杀掉，再杀掉旧进程，再 reload supervisor，之后终于恢复正常。从这一波的操作可以看出：<br />
1. 当看到 unix:///tmp/supervisor.sock no such file 时，一定要好好排查，不要着急去启动。即：遇到问题，不要想着重启！<br />
2. 可以使用一些更优雅的方式来运行 supervisor。比如系统是 CentOS，你把 supervisor 做成一个 systemctl 服务就能避免这些问题。<br />
3. reload supervisor 之后，一定记得检查下，是不是只有一个进程在跑！</p>
<p>此案例原始文章在 https://blog.csdn.net/u012375924/article/details/84946987。</p>
<p>另外，之前一直看有人说 Supervisor 不支持 Python 3，刚才去官网查看更新记录，可以看到从版本 4.0 开始，Supervisor 已经开始支持 Python 3（最低版本要求 3.4）。</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.somedoc.net/2020/01/03/supervisor-%e4%b8%8a%e6%89%8b/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
