0

I'm new to django and I've installed django-mailer 2.0. It's working when I manually send the queued mails: (venv)$ python manage.py send_mail, but when I set up the crontab (which is the first time I use a cron job), it's not working. I guess there might be some mistakes in the paths.

Official documentation of django-mailer suggests:
* * * * * (/path/to/your/python /path/to/your/manage.py send_mail >> ~/cron_mail.log 2>&1)

Mine:

# first I tried:
* * * * * (/usr/bin/python3 /Users/username/Documents/GitHub/projectname/manage.py send_mail >> ~/cron_mail.log 2>&1)

# then I tried:
* * * * * (/Users/username/Documents/GitHub/projectname/venv/bin/python /Users/username/Documents/GitHub/projectname/manage.py send_mail >> ~/cron_mail.log 2>&1)

# also this:
* * * * * cd /Users/username/Documents/GitHub/projectname; venv/bin/python manage.py send_mail

# I've tried Romeo's solution as well

None is working.. Help please!

However, when I use exactly the same command in bash, it works:

$ cd /Users/username/Documents/GitHub/projectname
$ venv/bin/python manage.py send_mail

this works indeed! I'm utterly confused...

PS. about django-mailer: I manually sent 4 queued emails each to 2 email addresses, however, 2 got missing never delivered (not lost in junk mails either). Is this normal?

LD8
  • 176
  • 3
  • 14

1 Answers1

1

What you can try is to change to the directory where the code is located and then run it:

* * * * * cd /Users/username/Documents/GitHub/projectname; /usr/bin/python3 manage.py send_mail >>  /Users/username/cron_mail.log 2>&1

Also is better to use absolute paths in cron. And add this in script plus import your environment variables.:

#!/bin/bash
source /Users/username/.bash_profile #or .bashrc
cd /Users/username/Documents/GitHub/projectname
venv/bin/python manage.py send_mail >>  /Users/username/cron_mail.log 2>&1

and then make the script executable:

chmod +rx script.sh

and add it in cron:

* * * * * /path/to/script.sh
Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
  • thanks for the reply. I tried both, they haven't worked yet. In your first solution, after changed to the directory, should I use the python in the virtual env? or global python? Because I tried in bash that `venv/bin/python manage.py send_mail` is working, however, I repeat exactly the same code in crontab, it isn't. This is what I tried in bash: `* * * * * cd /Users/username/Documents/GitHub/projectname; venv/bin/python manage.py send_mail` – LD8 Feb 28 '20 at 11:14
  • @DL8 use script and source your .bash_profile (or .bashrc). Will edit my answer with your command – Romeo Ninov Feb 28 '20 at 11:15
  • Thanks for the update, however, it's still not working... I've tried everything. Don't know where could go wrong – LD8 Feb 28 '20 at 11:51
  • @DL8, i forgot one `/` when source the profile. Can you try again please – Romeo Ninov Feb 28 '20 at 11:57
  • Erm... still not working. When you say `chmod +rw script.sh` this command makes `script.sh` executable, does it mean after running this command, the `script.sh` file will be able to run as a command in bash? I hope not, because when I run this file directly, it says command not found – LD8 Feb 28 '20 at 12:07
  • @DL8, yes, it become command, you can run it. But if you are in current directory (where the script is located) you should run it as `./script.sh` – Romeo Ninov Feb 28 '20 at 12:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208701/discussion-between-dl8-and-romeo-ninov). – LD8 Feb 28 '20 at 12:09