Django crontab not executing test function
Question:
I have following:
-
python manage.py crontab show # this command give following
Currently active jobs in crontab:
12151a7f59f3f0be816fa30b31e7cc4d -> ('*/1 * * * *', 'media_api_server.cron.cronSendEmail')
-
My app is in virtual environment (env) active
-
In my media_api_server/cron.py I have following function:
def cronSendEmail():
print("Hello")
return true
-
In settings.py module:
INSTALLED_APPS = (
......
'django_crontab',
)
CRONJOBS = [
('*/1 * * * *', 'media_api_server.cron.cronSendEmail')
]
To me all defined in place but when I run python manage.py runserver in virtual environment, console doesn’t print any thing.
System check identified no issues (0 silenced).
July 26, 2016 - 12:12:52
Django version 1.8.1, using settings 'mediaserver.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
‘Django-crontab’ module is not working I followed its documentation here https://pypi.python.org/pypi/django-crontab
Answers:
Your code actually works. You may be think that print("Hello")
should appear in stdout? So it doesn’t work that way, because cron doesn’t use stdour
and stderr
for it’s output. To see actual results you should point path to some log file in CRONJOBS
list: just put '>> /path/to/log/file.log'
as last argument, e.g:
CRONJOBS = [
('*/1 * * * *', 'media_api_server.cron.cronSendEmail', '>> /path/to/log/file.log')
]
Also it might be helpful to redirect your errors to stdout too. For this you heed to add CRONTAB_COMMAND_SUFFIX = '2>&1'
to your settings.py
Try to change the crontab with a first line like:
SHELL=/bin/bash
Create the new line at crontab with:
./manage.py crontab add
And change the line created by crontab library with the command:
crontab -e
*/1 * * * * source /home/app/env/bin/activate && /home/app/manage.py crontab run 123HASHOFTASK123
source
sometimes not working in shell scripts
use .
(dot) instead
*/1 * * * * . /home/app/env/bin/activate e.t.c.
An alternative way, directly in the OS( in case you are running on a Unix system), so that you won’t have to fiddle with python libraries. Provided your OS user has all permissions/owns the django project, you can do it on the command line and in the directory where your project folder is;
user@server:~$ crontab -l
0 6 * * * python PROJECT/manage.py shell -c "<--import statements; function calls;-->"
Just sharing, cause recently I’ve had similar such situation and this turned out be a clean workaround
@swapnil-pingle’s answer worked for me in macOS Monterey as well.
Mentioning steps here from the link.
1.
Mac -> System Preferences -> Security & Privacy -> Privacy -> Full Disk Access
press +
.
3.
select /usr/sbin/cron
location.
At last started my server again. and it worked.
Note:
-print() is not working in cron file.
-to check if cron is working, I updated data in DB obj.
I have following:
-
python manage.py crontab show # this command give following Currently active jobs in crontab: 12151a7f59f3f0be816fa30b31e7cc4d -> ('*/1 * * * *', 'media_api_server.cron.cronSendEmail')
-
My app is in virtual environment (env) active
-
In my media_api_server/cron.py I have following function:
def cronSendEmail(): print("Hello") return true
-
In settings.py module:
INSTALLED_APPS = ( ...... 'django_crontab', ) CRONJOBS = [ ('*/1 * * * *', 'media_api_server.cron.cronSendEmail') ]
To me all defined in place but when I run python manage.py runserver in virtual environment, console doesn’t print any thing.
System check identified no issues (0 silenced).
July 26, 2016 - 12:12:52
Django version 1.8.1, using settings 'mediaserver.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
‘Django-crontab’ module is not working I followed its documentation here https://pypi.python.org/pypi/django-crontab
Your code actually works. You may be think that print("Hello")
should appear in stdout? So it doesn’t work that way, because cron doesn’t use stdour
and stderr
for it’s output. To see actual results you should point path to some log file in CRONJOBS
list: just put '>> /path/to/log/file.log'
as last argument, e.g:
CRONJOBS = [
('*/1 * * * *', 'media_api_server.cron.cronSendEmail', '>> /path/to/log/file.log')
]
Also it might be helpful to redirect your errors to stdout too. For this you heed to add CRONTAB_COMMAND_SUFFIX = '2>&1'
to your settings.py
Try to change the crontab with a first line like:
SHELL=/bin/bash
Create the new line at crontab with:
./manage.py crontab add
And change the line created by crontab library with the command:
crontab -e
*/1 * * * * source /home/app/env/bin/activate && /home/app/manage.py crontab run 123HASHOFTASK123
source
sometimes not working in shell scripts
use .
(dot) instead
*/1 * * * * . /home/app/env/bin/activate e.t.c.
An alternative way, directly in the OS( in case you are running on a Unix system), so that you won’t have to fiddle with python libraries. Provided your OS user has all permissions/owns the django project, you can do it on the command line and in the directory where your project folder is;
user@server:~$ crontab -l
0 6 * * * python PROJECT/manage.py shell -c "<--import statements; function calls;-->"
Just sharing, cause recently I’ve had similar such situation and this turned out be a clean workaround
@swapnil-pingle’s answer worked for me in macOS Monterey as well.
Mentioning steps here from the link.
1.
Mac -> System Preferences -> Security & Privacy -> Privacy -> Full Disk Access
press +
.
3.
select /usr/sbin/cron
location.
At last started my server again. and it worked.
Note:
-print() is not working in cron file.
-to check if cron is working, I updated data in DB obj.