Unable to make a function call using Django-cron

﹥>﹥吖頭↗ 提交于 2020-08-09 18:28:27

问题


I want to call a method once in a week for which I made an implementation as mentioned here

https://gutsytechster.wordpress.com/2019/06/24/how-to-setup-a-cron-job-in-django/

I am not sure how it works, but let me explain what I did. I need to call a method as mentioned in folder structure below.

proj_application
|
|- myapp
       |
       |-views.py (Method call Inside)
                |- poll_tracked()

In views.py,

def poll_tracked():
    print('called')

In settings.py, I have mentioned

INSTALLED_APPS = [
'django_crontab',
]

CRONJOBS = [
    ('* * * * *', 'myapp.views.poll_tracked', '>>' + os.path.join(BASE_DIR, 'data.log'))
]

After then I run

python3.7 manage.py crontab add
python3.7 manage.py runserver

When I run crontab -l, I can see,

* * * * * /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/domain/dashboard/proj_application/manage.py crontab run dceca84af9ceab8a4d39d08fa148969f >>/Users/domain/dashboard/proj_application/data.log # django-cronjobs for proj_application

A new log file called data.log is generated but the method poll_tracked() mentioned is not called and the logs is empty.

Has anyone faced this problem before? If so, Any help is appreciated. Thanks.


回答1:


Maybe just try adding 2>&1 at the end, it redirects the error output to the standard output, and might explain why your log file is empty. Also you are missing a space after >>

CRONJOBS = [
    ('* * * * *', 'myapp.views.poll_tracked', '>> ' + os.path.join(BASE_DIR, 'data.log') + ' 2>&1')
]


来源:https://stackoverflow.com/questions/62884939/unable-to-make-a-function-call-using-django-cron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!