问题

非root用户默认无法使用supervisor,因为普通用户是不能通过apt-get install supervisor 或 yum install supervisor安装supervisor的。

解决方案

既然supervisor是python实现的,那么在python虚拟环境中是可以直接安装的。

假如有一个通过conda创建的虚拟环境crawl,实现的具体步骤如下:

1. 启动虚拟环境:

conda activate crawl

2. 安装supervisor:

pip install supervisor

3.  创建supervisor的工作目录:

mkdir -m 755 -p /home/txwst/supervisor

4. 进入工作目录后,生成默认配置文件supervisor.conf:

echo_supervisord_conf > supervisord.conf

5. 修改配置文件的最后两行为如下内容:

[include]
files = ./conf.d/*.ini

记得在工作目录下创建conf.d目录,用于存放各程序的配置

6. 启动supervisord服务:

~/.conda/envs/crawl/bin/supervisord -c /home/txwst/supervisor/supervisord.conf -d /home/txwst/supervisor -q /home/txwst/supervisor

 

测试supervisor服务

1. 在目录/home/txwst/supervisor/conf.d下创建scrapyd的配置文件scrapyd.ini;

前提是已在crawl环境中安装了scrapyd

具体内容为:

[program:scrapyd]
directory=/home/txwst/scrapyd
command=/home/txwst/.conda/envs/crawl/bin/scrapyd
autostart=true
autorestart=true
startsecs=5


stderr_logifle=/tmp/scrapyd_stderr.log
stdout_logfile=/tmp/scrapyd_stdout.log
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=10

2. 重新载入supervisor配置:

~/.conda/envs/crawl/bin/supervisorctl -c /home/txwst/supervisor/supervisord.conf reload

3. 测试scrapyd是否启动:

[txwst@tx01 ~]$ curl localhost:6800

<html>
<head><title>Scrapyd</title></head>
<body>
<h1>Scrapyd</h1>
<p>Available projects: <b></b></p>
<ul>
<li><a href="/jobs">Jobs</a></li>

<li><a href="/logs/">Logs</a></li>
<li><a href="http://scrapyd.readthedocs.org/en/latest/">Documentation</a></li>
</ul>

<h2>How to schedule a spider?</h2>

<p>To schedule a spider you need to use the API (this web UI is only for
monitoring)</p>

<p>Example using <a href="http://curl.haxx.se/">curl</a>:</p>
<p><code>curl http://localhost:6800/schedule.json -d project=default -d spider=somespider</code></p>

<p>For more information about the API, see the <a href="http://scrapyd.readthedocs.org/en/latest/">Scrapyd documentation</a></p>
</body>
</html>

总结

这里通过在虚拟环境中安装supervisor,然后自己创建配置文件,并启动相应服务,最终完成supervisor在独立环境中运行的目标。

如有其他问题,欢迎留言。