APScheduler定时执行外加supervisor管理后台运行
最近写的天气爬虫想要让它在后台每天定时执行,一开始用的celery,但不知道为什么明明设置cron在某个时间运行,但任务却不间断的运行。无奈转用apscheduler,但是不管怎么设置都不能使得当调用: python tasks.py 的时候都会阻塞在控制台。再次无奈转用supervisor。
首先是任务tasks.py:
#-*- coding: utf-8 -*-
#!/usr/bin/python import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
from scrapy.crawler import CrawlerProcess
from province_spider import ProvinceSpider
from billiard import Process from scrapy.utils.log import configure_logging
configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s', 'LOG_FILE': 'schedule.log'}) def _crawl(path=None):
crawl = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
crawl.crawl(ProvinceSpider)
crawl.start()
crawl.stop() def run_crawl(path=None):
p = Process(target=_crawl, args=['hahahahha'])
p.start()
#p.join() scheduler = BlockingScheduler(daemon=True)
scheduler.add_job(run_crawl, "cron", hour=8, minute=30, timezone='Asia/Shanghai')
scheduler.add_job(run_crawl, "cron", hour=12, minute=30, timezone='Asia/Shanghai')
scheduler.add_job(run_crawl, "cron", hour=18, minute=30, timezone='Asia/Shanghai') try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()
设置成8点半、12点半、18点半是因为天气数据是一天更新三次,分别在8点,12点,18点,有图为证:

直接执行:python tasks.py可以执行任务,但是会在控制台阻塞。这个时候要用supervisor。
ubuntu安装: apt-get install supervisor
开始:
1. 进行/etc/supervisor/conf.d 目录,新建weather_aps.conf文件,文件内容为:
[program:weather_aps]
command=python /var/my_git/WeatherCrawler/aps/tasks.py
autorstart=true
stdout_logfile=/var/my_git/WeatherCrawler/aps/log/weather_aps.log
2. 启动supervisor:
/etc/init.d/supervisor start
3. 启动成功后,查看weather_aps的状态:
supervisorctl status weather_aps
如果是running,则表示成功.
需要注意的是,如果在任务里面有日志输出到文件,而文件没有指定绝对路径的话,默认是在根目录生成,即在 ” / “ 目录下。
APScheduler定时执行外加supervisor管理后台运行的更多相关文章
- Supervisor 管理后台守护进程
Supervisor 管理后台守护进程 参考原文如下: http://codinn.com/people/brant/notes/110948/ 做了一些注释 +++++++++++引用开始+++++ ...
- Linux进程后台执行nohup(OpenTSDB后台运行方法)
1.问题描述 OpenTSDB执行./tsdb tsd启动之后,占有控制台执行并且Ctrl+C后就退出了,关闭控制台同样会退出. 2.解决方法(在/opt/module/opentsdb-2.3.1/ ...
- 使用supervisor管理进程
Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...
- supervisor管理进程 superlance对进程状态报警
supervisor介绍 首先,介绍一下supervisor.Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linu ...
- supervisor管理进程工具配置
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...
- 使用supervisor管理后台进程
在linux中supervisor是用来管理后台进程的,是一个用python写的进程管理工具,可以让宕机的进程重启.这里我们大概讲一下用他来管理uWSGI. 一.安装supervisor 1.pyth ...
- python脚本后台运行
问题描述: 环境: CentOS6.4 一个用python写的监控脚本test1.py,用while True方式一直运行,在ssh远程(使用putty终端)时通过以下命令启动脚本: python t ...
- linux 后台运行命令 nohup命令
转载:http://if.ustc.edu.cn/~ygwu/blog/archives/000538.html 2005年04月18日 简单而有用的nohup命令在UNIX/LINUX中,普通进程用 ...
- 让PHP程序永远在后台运行
PHP里有个函数很有用.这是在最近的开发中才逐渐用到的. int ignore_user_abort ( [bool setting] ) 这个函数的作用是指示服务器端在远程客户端关闭连接后是否继续执 ...
随机推荐
- RabbitMQ使用笔记
一.安装 1.下载所需安装包 下载服务端(原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的):http://www.rabbitmq.com/install-windows.htm ...
- n位格雷曼实现
参考: 格雷码的实现 问题:产生n位元的所有格雷码. 格雷码(Gray Code)是一个数列集合,每个数使用二进位来表示,假设使用n位元来表示每个数字,任两个数之间只有一个位元值不同. 例如以下为 ...
- 在dell服务器上装windows server 2012详细解析
壹: 首先确定磁盘阵列的问题,在dell服务器开机后按住 Ctrl+R 或者 F2 会展开虚拟磁盘创建菜单 详细步骤可以查看:https://jingyan.baidu.com/article/915 ...
- Internet History, Technology and Security (Week 5-2)
Week 5 (续) Layer 2: Internet Protocol The InterNetwork (IP) 老师强调了一下不用去记住他介绍的人所说的每句话,而是记住要点,了解老师所做的PP ...
- 第四周PSP&进度条
团队项目PSP 一:表格 C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论开发环境.工具以及技术 8:37 10:42 25 10 ...
- TP5 助手函数与TP3.2单字母函数
一.TP5 助手函数 助手函数 描述 abort 中断执行并发送HTTP状态码 action 调用控制器类的操作 cache 缓存管理 config 获取和设置配置参数 controller 实例化控 ...
- 基于 Jmeter 的 web 端接口自动化测试平台
简介 基于之前的react+unittest+flask的接口自动化平台开发经验,和趟过的坑,我重新开发了这个接口自动化平台.平台前端采用的antd+dva+umi的antd-pro模板,结合平台业务 ...
- [转帖]TLS 1.3概述
TLS 1.3概述 http://www.inforsec.org/wp/?p=1960 By Yanting Yang | 四月 26, 2017| SecComm 作者:李新宇 中科院软件所 ...
- CodeForces - 988D(思维STL)
原文地址:https://blog.csdn.net/weixin_39453270/article/details/80548442 博主已经讲的很好了 题意: 从一个序列中,选出一个集合,使得集合 ...
- ELK5.4安装Xpack
X-Pack是一个Elastic Stack的扩展,将安全,警报,监控,报告和图形功能包含在一个易于安装的软件包中.在Elasticsearch 5.0.0之前,必须安装单独的Shield.Watch ...