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] ) 这个函数的作用是指示服务器端在远程客户端关闭连接后是否继续执 ...
随机推荐
- KEIL C51程序中如何嵌入汇编
模块内接口:使用如下标志符:#pragma asm汇编语句#pragma endasm注意:如果在c51程序中使用了汇编语言,注意在Keil编译器中需要激活Properties中的“Generate ...
- 第八周PSP&进度条
团队项目PSP 一.表格: C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论各个模块页面设计 9:30 12:30 站立会议 分配 ...
- epel [Errno 14] problem making ssl connection
问题描述: 执行yum命令时,报错[Errno 14] problem making ssl connection 问题分析: ssl证书问题 问题解决: sed -i 's/^#baseurl/ba ...
- 命令行执行python文件时提示ImportError: No module named 'xxx'
背景: 最近在写接口自动化测试框架的时候发现,框架使用pycharm ide的时候可以正常跑测试用例,但是在dos窗口输入命令执行测试的时候,import项目内部的包时报错“ModuleNotFoun ...
- [转帖]nvidia nvlink互联与nvswitch介绍
nvidia nvlink互联与nvswitch介绍 https://www.chiphell.com/thread-1851449-1-1.html 差不多在一个月前在年度gtc会议上,老黄公开了d ...
- Qt宏Q_OBJECT展开记录
c++中的宏 #define A 3.14 展开后把A替换为B #define A(a) #a 展开后用a的字符串替换A(a) #define A(a) a##B 展开后把a和B的字符串连接起来后替换 ...
- ACM数论之旅5---数论四大定理(你怕不怕(☆゚∀゚)老实告诉我)
(本篇无证明,想要证明的去找度娘)o(*≧▽≦)ツ ----------数论四大定理--------- 数论四大定理: 1.威尔逊定理 2.欧拉定理 3.孙子定理(中国剩余定理) 4.费马小定理 (提 ...
- C# 房贷计算器
设计背景 百度小程序中的房贷计算器不能满足我个人的需求,故而开发一个.NET小程序.希望后期能用JS重写,发布在网上供大家使用. 设计思路 根据百度公式:等额本息月还款 = [贷款本金×月利率×(1+ ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Win10系统 安装Anaconda+TensorFlow+Keras
小白一枚,安装过程走了很多坑,前前后后安装了好几天,因此记录一下. 一.安装anaconda 官方下载地址:https://repo.continuum.io/archive/ 选项相应的版本安装,我 ...