我正在尝试让django-cron工作,但它不是。我按照here的说明设置了我的cron,但问题是我的作业只在我在命令行输入python manage.py runcrons时运行,而且作业不是每5分钟运行一次。我不知道还能做什么。我已经阅读了关于crontabs和chronograph的其他文档,但我感到困惑。我是将crontabs与cron一起安装,还是将cron与django-cron一起安装?另外,我如何让我的作业自动运行。在文档here中,我阅读了Now everytime you run the management command python manage.py runcrons all the crons will run if required. Depending on the application the management command can be called from the Unix crontab as often as required. Every 5 minutes usually works for most of my applications.。这是什么意思。我在这里错过了什么。我迷路了。帮助
Settings.py
CRON_CLASSES = (
"myapp.views.MyCronJob",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_cron',
'django.contrib.admin',
'django.contrib.admindocs',
'myapp',
)views.py
from django_cron import CronJobBase, Schedule
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 10 # every 10 min
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'my_app.my_cron_job' # a unique code
def do(self):
print "10 min Cron"
theJob()我应该提一下,我正在windows平台上使用pycharm来运行django……
发布于 2013-04-13 10:08:00
问题的根源在于操作系统。Webserver不是那种调用cronjob的守护程序,它只是处理web请求。要在windows上调用定期任务,您需要使用Windows任务计划程序:
What is the Windows version of cron?
另一种解决问题的方法是在芹菜节拍模式下启动芹菜守护进程。
http://celeryproject.org/
http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html
这是一种比较困难的方法,如果你有一个非常简单的应用程序,你不需要使用芹菜。但在很多情况下,队列是最好的解决方案。
发布于 2016-11-08 00:49:41
改为安装django-crontab。
pip install django-crontab 更改您的settings.py以包含django-crontab
INSTALLED_APPS = (
'django_crontab',
...
)
CRONJOBS = [
('*/5 * * * *', 'myproject.cron.my_scheduled_job')
]在应用程序目录中创建cron.py文件
def my_scheduled_job():
#do something每次包含或更新cron作业时都运行此命令。
python manage.py crontab add运行本地服务器以测试cron:
python manage.py runserver您的工作完成了!:)
发布于 2017-11-19 01:22:25
您可以以编程方式执行'runcrons‘,如下所示:
from django.core.management import call_command
call_command('runcrons')例如,我将上面的几行代码放入我的wsgi.py中
https://stackoverflow.com/questions/15981735
复制相似问题