[Fixed]-Django crontab not executing test function

24👍

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

2👍

@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.

1👍

source sometimes not working in shell scripts

use . (dot) instead

*/1 * * * * . /home/app/env/bin/activate e.t.c.

0👍

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
👤TxeMac

0👍

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

Leave a comment