1. 复习常用命令
virtualenv
$ mkvirtualenv even
$ workon even
$ deactivate
$ rmvirtualenv even
$ cdvirtualenv # 进入到虚拟环境所在的目录
$ pwd # 查看当前绝对路径
Supervisor
$ supervisord -c /etc/supervisord.conf
$ supervisorctl reload
$ supervisorctl status
$ supervisorctl start 进程名
$ supervisorctl stop 进程名
kill 进程,直接 kill python进程
$ ps -aux|grep python
$ kill -9 12345
2. 相关踩坑
① Supervisor 使用相关
a. 利用虚拟环境执行python脚本
直接调用虚拟环境下的python解释器
托管给 supervisor 的命令如下:
/root/.PythonVirtualenvs/AutoCpdaily/bin/python /home/xiongyi/python/project/AutoCpdaily/main.py
有时也不是调用python解释器,比如调用uwsgi:
/root/.PythonVirtualenvs/QiDianSpider/bin/uwsgi --ini /home/xiongyi/python/project/QiDianSpider/uwsgi.ini
b. 一些报错
参考文章:Supervisor 安装配置
Unlinking stale socket /var/run/supervisor/supervisor.sock
解决:
执行 unlink /var/run/supervisor/supervisor.sock
注意路径
c. Web 管理页面
在 /etc/supervisord.conf 中配置:
[inet_http_server] ; inet (TCP) server disabled by default
port = *:9001 ; ip_address:port specifier, *:port for all iface
username = root ; default is no username (open server)
password = 123456 ; default is no password (open server)
外网 ip 用 * 代替
配置完成后记得重启supervistor
$ ps -aux|grep supervisor
$ kill -9 12345
$ supervisor -c /etc/supervisord.conf
记得开防火墙
② 路径相关
a. 这绝对是最坑爹的一点,注意!Windows中路径用 \ 分割,而 linux 用的是 /
b. 当报配置文件未找到时,使用绝对路径引用
# 配置文件用绝对路径读入
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
cf = configparser.ConfigParser()
cf.read(path + '/new/config.ini')
c. 日志输出在执行目录下,若使用 supervisor 启动,则在 /root 目录下,故须指定目录
logPath = cf.get("LOG", "logPath")
file_handler = logging.FileHandler(logPath + 'cpdaily.log', encoding='utf-8')