问题
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