我在使用我的用户crontab调度manage.py celery call myapp.tasks.mytask
时遇到问题,因为当cron尝试运行该作业时,它会在stderr中获取此信息(以/var/mail/kal
的形式邮寄给我)
Unknown command: 'celery'
Type 'manage.py help' for usage.
相同的命令完全来自常规的bash登录shell,但它在crontab中不起作用。
我在Debian上做这件事:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.0 (wheezy)
Release: 7.0
Codename: wheezy
我在StackOverflow上阅读了许多类似的问题,并尝试了许多建议的解决方案。到目前为止,他们都没有为我工作过。以下是我迄今为止尝试过的解决方案:
首先,我确保在crontab中指定相关的环境变量:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
注意:这些都在以下所有解决方案中保留。
* * * * * /home/kal/.virtualenvs/foo_dev/bin/python /home/kal/foo/manage.py celery call myapp.tasks.mytask
* * * * * cd /home/kal/foo && /home/kal/.virtualenvs/foo_dev/bin/python ./manage.py celery call myapp.tasks.mytask
〜/ 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之间的输出,它们完全相同:
来自cron作业的输出:
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的输出:
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
我完全不知所措。
答案 0 :(得分:3)
我找到了问题的原因。
非常 非常微妙。
问题有两个:
USER
环境变量;仅LOGNAME
; manage.py
运行且指定了管理命令时,如果在导入设置模块期间引发异常,则Django 静默会故障转移到空白设置我的设置模块试图引用{cn}环境中不存在的os.environ['USER']
。因此,导入设置模块会导致引发异常,并且Django会悄悄地故障转移到空白设置,这意味着空白INSTALLED_APPS
并且没有celery
命令!
答案 1 :(得分:2)
忘掉cron。 Use the celerybeat_scheduler
答案 2 :(得分:0)
确保djcelery
位于 settings.py 的INSTALLED_APPS
中,如https://pypi.python.org/pypi/django-celery所示
INSTALLED_APPS += ("djcelery", )
import djcelery
djcelery.setup_loader()
答案 3 :(得分:0)
您可以使用fabric来启动并运行它。您的virtualenv可能不会激活,但使用像fabric这样的工具发出命令
def reset_app_local(appname):
run("source ~/env/bin/activate && python manage.py celery call myapp.tasks.mytask ",shell="/bin/bash")
如果在本地服务器上运行,请运行以下命令。
def reset_app_local(appname):
local("source ~/env/bin/activate && python manage.py celery call myapp.tasks.mytask ",shell="/bin/bash")