Cron和virtualenv [英] Cron and virtualenv
问题描述
我试图从cron运行Django管理命令。我使用virtualenv保持我的项目沙箱。
I am trying to run a Django management command from cron. I am using virtualenv to keep my project sandboxed.
我在这里和其他地方看到示例运行管理命令从virtualenv的像:
I have seen examples here and elsewhere that show running management commands from within virtualenv's like:
0 3 * * * source /home/user/project/env/bin/activate && /home/user/project/manage.py command arg
但是,即使syslog显示一个条目任务应该已经启动,此任务从未实际运行(脚本的日志文件为空)。如果我从shell手动运行行,它的工作原理。
However, even though syslog shows an entry when the task should have started, this task never actually runs (the log file for the script is empty). If I run the line manually from the shell, it works as expected.
我现在可以通过cron运行命令的唯一方法是将命令断开,并将它们放在一个笨拙的bash封装脚本中:
The only way I can currently get the command to run via cron, is to break the commands up and put them in a dumb bash wrapper script:
#!/bin/sh
source /home/user/project/env/bin/activate
cd /home/user/project/
./manage.py command arg
请告诉我是。
感谢
编辑:
ars想出了一个有效的命令组合:
ars came up with a working combination of commands:
0 3 * * * cd /home/user/project && /home/user/project/env/bin/python /home/user/project/manage.py command arg
至少在我的情况下,调用virtualenv的激活脚本什么也没做。
At least in my case, invoking the activate script for the virtualenv did nothing. This works, so on with the show.
推荐答案
你应该能够通过使用 python
在您的虚拟环境中:
You should be able to do this by using the python
in your virtual environment:
/home/my/virtual/bin/python /home/my/project/manage.py command arg
编辑:如果你的django项目不在PYTHONPATH ,那么您需要切换到正确的目录:
If your django project isn't in the PYTHONPATH, then you'll need to switch to the right directory:
cd /home/my/project && /home/my/virtual/bin/python ...
您也可以尝试记录失败来自cron:
You can also try to log the failure from cron:
cd /home/my/project && /home/my/virtual/bin/python /home/my/project/manage.py > /tmp/cronlog.txt 2>&1
在您的 manage.py
脚本中:
#!/home/my/virtual/bin/python
这篇关于Cron和virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!