1

I'm using Django Post Office to send emails in my Django 1.6.2 application but I'm having trouble getting DPO's 'send_queued_mail' command to execute from a crontab file. I want the command to execute every minute. This was my first cron job (owned by the root user):

$ sudo crontab -e
* * * * * . /home/myproj/venv/swing/bin/activate && /home/myproj/venv/myproj/bin/python /www/myproj/manage.py send_queued_mail >> /var/log/send_mail.log 2>&1

When I examine /var/log/syslog, I can see the command and it appears to be executing normally. However, I see this error in the send_mail.log file:

Unknown command: 'send_queued_mail'
Type 'manage.py help' for usage.

It occurred to me that the problem was that since this is root's cronjob, I'm trying to activate a virtual environment for root which does not exist. So I moved the above command to the "myproj" user's crontab (this user owns the project) but I still get the same error.

I also created the following 'send-queued-mail-cronjob' script to execute the command within a virtual environment. This approach has been suggested here on SO to others having the same problem.

#!/bin/sh
cd /home/myproj
source venv/myproj/bin/activate
venv/myproj/bin/python /www/myproj/manage.py send_queued_mail >> /var/log/send_mail.log 2>&1

I call it via this cronjob owned by user 'myproj':

* * * * * /www/myproj/bin/send-queued-mail-cronjob

However, when it runs, I still get the same 'Unknown command error' as before.

Is there something else I need to do? I can execute the commands in the first cronjob from my command line without errors. I can also run the commands in the script from the command line when I log in as user 'myproj' and they run just fine.

Jim
  • 13,430
  • 26
  • 104
  • 155
  • Does `venv` work with `sh`? See: http://stackoverflow.com/questions/7369145/activating-a-virtualenv-using-a-shell-script-doesnt-seem-to-work – jmunsch Apr 16 '15 at 21:57
  • Unrelated, but I think you have a typo in your first cron job command: `2>&!` instead of `2>&1` – help_asap Apr 16 '15 at 21:59
  • @jmunsch I read the post you referred to but I don't have the virtualenv_activate.sh command on my server. The virtualenv commands were installed into /usr/local/bin and there's no such file there. – Jim Apr 16 '15 at 22:10
  • Have you searched `virtualenv_activate.sh` in your server just inside `/usr/local/bin` ? Have you tried looking for it in the _whole_ server (e.g. with commands like `find`, `which`, etc.) ? Just to be sure that you really don't have it. – help_asap Apr 17 '15 at 07:25
  • I've experimented with about eight different variations of a sh script file that will be called by cron to create a virtual environment that can recognize the send_queued_mail command but I have not been successful. – Jim Apr 17 '15 at 20:19
  • @help_asap Yes, I've searched the entire Debian server using "find" run by the root user. It's not there. – Jim Apr 17 '15 at 21:29
  • I just looked at Django Post Office repository and, maybe it's completely unrelated, but it is suggested to schedule the command to run via cron like this: `* * * * * (/usr/bin/python manage.py send_queued_mail >> send_mail.log 2>&1)`, that is using parentheses, so (if I recall correctly) actually _running everything in a subshell_ – help_asap Apr 17 '15 at 22:19
  • @help_asap That was the first thing I tried and it definitely does not work. I'm surprised it's even in the documentation. – Jim Apr 18 '15 at 02:53

1 Answers1

0

source command does not work with vanilla sh or Ubuntu's dash.

You need to use:

. venv/myproj/bin/activate

Instead of:

source venv/myproj/bin/activate

More info.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • I've tried using the Bash shell and its "." command but I still get the "Unknown command" error. – Jim Apr 16 '15 at 22:28