当我运行 python manage.py runcrons
时,cron作业成功执行(prints'执行')。但这不会自动执行。正如我在班上所说的那样,它应该每分钟执行。这是代码:
settings.py
CRON_CLASSES = [
"app.views.MyCronJob"
]
app.views
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 1
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'my_app.my_cron_job' # not sure what this is supposed to be?
def do(self):
print('Executed')
有什么想法?
您还需要设置cron的crontab条目:
> crontab -e
*/5 * * * * source /home/ubuntu/.bashrc && source /home/ubuntu/work/your-project/bin/activate && python /home/ubuntu/work/your-project/src/manage.py runcrons > /home/ubuntu/cronjob.log
取自官方文档
1,以获取Ayush的答案。这对我有用。
crontab -e
这将打开编辑器,在底部复制以下行,然后保存&关闭。
*/5 * * * * * source/home/ubuntu/.bashrc&&source/home/ubuntu/work/your-project/bin/activate&&python/home/ubuntu/work/your-project/src/manage.py runcrons>/home/ubuntu/cronjob.log
除此之外,请尝试在您的Django项目目录中重新编写这些内容:
python manage.py runcrons --force
service cron start