我正在尝试从cron运行Django管理命令。我使用virtualenv保持我的项目沙盒。
我在这里和其他地方看到了从virtualenv中运行管理命令的示例,例如:
0 3 * * * source /home/user/project/env/bin/activate && /home/user/project/manage.py command arg
然而,尽管syslog显示了任务应该在何时启动的条目,但该任务从未实际运行(脚本的日志文件为空)。如果我从shell中手动运行这一行,它将按预期工作。
我目前可以通过cron运行命令的唯一方法是将命令分解并将它们放在一个哑bash包装脚本中:
#!/bin/sh
source /home/user/project/env/bin/activate
cd /home/user/project/
./manage.py command arg
编辑:
Ars提出了一个命令的工作组合:
0 3 * * * cd /home/user/project && /home/user/project/env/bin/python /home/user/project/manage.py command arg
至少在我的例子中,调用virtualenv的激活脚本没有任何作用。这招管用,所以节目继续。
对我来说,最好的解决办法是两者兼得
使用venv bin/目录下的python二进制文件
设置python路径
要包含venv模块目录。
man python提到在shell中修改路径$PYTHONPATH或在python中修改sys.path
其他回答提到了使用shell来实现这一点的想法。从python中,向我的脚本中添加以下行可以让我成功地直接从cron运行它。
import sys
sys.path.insert(0,'/path/to/venv/lib/python3.3/site-packages');
这是它在互动会话中的样子
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python3.3', '/usr/lib/python3.3/plat-x86_64-linux-gnu', '/usr/lib/python3.3/lib-dynload']
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'
>>> sys.path.insert(0,'/path/to/venv/modules/');
>>> import requests
>>>
对我来说,最好的解决办法是两者兼得
使用venv bin/目录下的python二进制文件
设置python路径
要包含venv模块目录。
man python提到在shell中修改路径$PYTHONPATH或在python中修改sys.path
其他回答提到了使用shell来实现这一点的想法。从python中,向我的脚本中添加以下行可以让我成功地直接从cron运行它。
import sys
sys.path.insert(0,'/path/to/venv/lib/python3.3/site-packages');
这是它在互动会话中的样子
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python3.3', '/usr/lib/python3.3/plat-x86_64-linux-gnu', '/usr/lib/python3.3/lib-dynload']
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'
>>> sys.path.insert(0,'/path/to/venv/modules/');
>>> import requests
>>>
我很抱歉第n个答案,但我检查了答案,确实有更简单和整洁的答案。
长话短说
在你的cron中使用venv的python二进制文件:
0 3 * * * /home/user/project/env/bin/python /home/user/project/manage.py
很长的故事
We activate the virtual environment when we want to set the current shell with the python config of that specific virtual environment(that is binaries and modules of that).
It is relevant to work with the current shell : execute multiple python commands on the current shell without the need to reference the full python path of the venv.
In the frame of a cron or even a bash, which value to activate the environment ?
Besides I read in some answers some references to bash rather than sh or still to define a wrapper to call the Python code. But why the hell should we bother with these ?
重复一遍,照做就好:
0 3 * * * /home/user/project/env/bin/python /home/user/project/manage.py
文件证实:
你不需要特别激活一个环境;激活
只需将虚拟环境的二进制目录前置到您的路径,
这样" python "就会调用虚拟环境的python解释器
您可以运行已安装的脚本,而不必使用它们的全部
路径。但是,在虚拟环境中安装的所有脚本都应该
在不激活它的情况下也可以运行,并使用虚拟机运行
环境的Python自动。