Django管理命令在cron中不起作用 [英] Django management command won't work in cron
问题描述
我在与用户crontab一起安排 manage.py celery调用myapp.tasks.mytask
时遇到问题,因为当cron尝试运行作业时,它得到了在stderr中(该邮件以 / var / mail / kal
的形式发送给我)
I am having problems scheduling a manage.py celery call myapp.tasks.mytask
with my user crontab, in that when cron tries to run the job, it gets this in stderr (which gets mailed to me, as /var/mail/kal
)
Unknown command: 'celery'
Type 'manage.py help' for usage.
同一命令完全可以在常规bash登录shell中使用,但在crontab中不起作用。
The same command works completely from a regular bash login shell, but it won't work in crontab.
我正在Debian Wheezy上执行此操作:
I am doing this on Debian wheezy:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.0 (wheezy)
Release: 7.0
Codename: wheezy
我在StackOverflow上阅读了许多类似的问题,并尝试了许多建议的解决方案。到目前为止,他们都没有为我工作。以下是到目前为止我尝试过的解决方案:
I have read many similar questions on StackOverflow and tried many of the suggested solutions. None of them have worked for me so far. Here are the solutions I have tried so far:
首先,我确保在crontab中指定相关的环境变量:
First, I made sure to specify relevant environment variables in the crontab:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
注意:这些在以下所有解决方案中都保持不变。
NOTE: these stay in place in all of the following solutions.
* * * * * /home/kal/.virtualenvs/foo_dev/bin/python /home/kal/foo/manage.py celery call myapp.tasks.mytask
2。 cd首先进入项目路径
2. cd'ing into the project path first
* * * * * cd /home/kal/foo && /home/kal/.virtualenvs/foo_dev/bin/python ./manage.py celery call myapp.tasks.mytask
3。用bash脚本包装所有内容
〜/ mytask.sh的内容:
3. Wrapping everything in a bash script
Content of ~/mytask.sh:
#!/usr/bin/env bash
source /home/kal/.virtualenvs/foo_dev/bin/activate;
cd /home/kal/foo;
./manage.py celery call myapp.tasks.mytask;
crontab行:
* * * * * ~/mytask.sh
我什至修改了 myproj / settings.py
输出 sys.path
和 sys.executable
到stderr并比较了cron和登录shell之间的输出,它们是完全相同的:
I even modified myproj/settings.py
to output sys.path
and sys.executable
to stderr and compared the output between cron and the login shell, and they are exactly the same:
cron作业的输出:
Output from cron job:
sys.executable:
/home/kal/.virtualenvs/foo_dev/bin/python
Content of sys.path:
/home/kal/foo
/home/kal/.virtualenvs/foo_dev/src/bootstrap
/home/kal/.virtualenvs/foo_dev/src/django-json-rpc
/home/kal/.virtualenvs/foo_dev/lib/python2.7
/home/kal/.virtualenvs/foo_dev/lib/python2.7/plat-linux2
/home/kal/.virtualenvs/foo_dev/lib/python2.7/lib-tk
/home/kal/.virtualenvs/foo_dev/lib/python2.7/lib-old
/home/kal/.virtualenvs/foo_dev/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/home/kal/.virtualenvs/foo_dev/local/lib/python2.7/site-packages
/home/kal/foo
Bash登录shell的输出:
Output from Bash login shell:
sys.executable:
/home/kal/.virtualenvs/foo_dev/bin/python
Content of sys.path:
/home/kal/foo
/home/kal/.virtualenvs/foo_dev/src/bootstrap
/home/kal/.virtualenvs/foo_dev/src/django-json-rpc
/home/kal/.virtualenvs/foo_dev/lib/python2.7
/home/kal/.virtualenvs/foo_dev/lib/python2.7/plat-linux2
/home/kal/.virtualenvs/foo_dev/lib/python2.7/lib-tk
/home/kal/.virtualenvs/foo_dev/lib/python2.7/lib-old
/home/kal/.virtualenvs/foo_dev/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/home/kal/.virtualenvs/foo_dev/local/lib/python2.7/site-packages
/home/kal/foo
我完全感到困惑。
推荐答案
我找到了问题的原因。
它非常非常 非常。
问题有两个:
- 没有
USER
cron作业中的环境变量;仅LOGNAME
; - 在下运行
manage.py
时一个指定的管理命令,如果在导入设置模块期间引发异常,则Django 安静地会故障转移为空白设置。
- There is no
USER
environment variable in a cron job; onlyLOGNAME
; - When
manage.py
is run with a management command specified, Django quietly fails over to blank settings if an exception is raised during the import of the settings module.
我的设置模块正在尝试引用 os.environ ['USER']
,在cron的环境中不存在。因此,导入设置模块会引发异常,而Django会悄悄地故障转移为空白设置,这意味着空白 INSTALLED_APPS
且没有 celery
命令!
My settings module was trying to reference os.environ['USER']
, which doesn't exist in cron's environment. So importing the settings module causes an exception to be raised, and Django quietly fails over to blank settings, which means blank INSTALLED_APPS
and no celery
command!
这篇关于Django管理命令在cron中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!