我正在尝试从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的激活脚本没有任何作用。这招管用,所以节目继续。
我也遇到过同样的问题,并花了很多时间去解决它。
这里没有一个解决方案对我有帮助,所以我分享了对我有用的方法:
在项目目录中打开一个新文件“pick_name.sh”。
在"pick_name.sh"文件中,写入并保存以下代码行:
#!/bin/bash
source /YOUR_VIRTUAL_ENV_PATH/bin/activate
export PYTHONPATH="${PYTHONPATH}:/PATH_TO_CUSTOM_MODULE_YOU_CREATED**OPTIONAL**"
export PYTHONPATH="${PYTHONPATH}:/PATH_TO_ANOTHER_CUSTOM_MODULE_YOU_CREATED**OPTIONAL**"
cd /PATH_TO_DIR_STORING_FILE_NAME.PY
python file_name.py
转到/var/spool/cron/crontabs(或到您的cron管理文件所在的位置)并打开“根”文件。
将这些行添加到crontab文件夹中的根文件中:
# m h dom mon dow command
* * * * * /PATH_TO_DIR_WHERE_PICK_NAME.SH_SITS/pick_name.sh >> /YOUR_PROJECT_DIR/cron_output.txt 2>&1
注:
This command (section 4.) will run the "pick_name.sh" file.
In this example it runs every minute, so make sure you change it according to your needs.
It writes all logs to a log file called "cron_ouput".
No need to create the file before, it will be created automatically.
Make sure to replace all paths (I wrote them in capital letters) to your paths.
You can change file names, if so, make sure to change it in all appearances in my instructions to avoid errors.
If you want to add another py file to run by cron, you need to add it to the "pick_nam.sh" file* not to the cron. Simply duplicate section 2. lines in the "pick_nam.sh" but without the "#!/bin/bash" part.
Then, every time the cron will run "pick_name.sh" it will run all the files you specified inside of it.
Make sure to restart cron after changes, it could have saved me a lot of debugging time, use this command:
systemctl restart cron
你应该能够通过在虚拟环境中使用python来做到这一点:
/home/my/virtual/bin/python /home/my/project/manage.py command arg
编辑:如果你的django项目不在PYTHONPATH目录下,那么你需要切换到正确的目录:
cd /home/my/project && /home/my/virtual/bin/python ...
您也可以尝试从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在它自己的最小sh环境中执行,下面是我在虚拟环境中运行Python脚本的方法:
* * * * * . ~/.bash_profile; . ~/path/to/venv/bin/activate; python ~/path/to/script.py
(注:如果。~ /。Bash_profile不适合您,那么请尝试一下。~ /。Bashrc或。~ /。配置文件取决于您的服务器如何设置。)
这将加载您的bash shell环境,然后激活您的Python虚拟环境,基本上为您留下与测试脚本相同的设置。
不需要在crontab中定义环境变量,也不需要修改现有脚本。
python脚本
from datetime import datetime
import boto # check wheather its taking the virtualenv or not
import sys
param1=sys.argv[1] #Param
myFile = open('appendtxt.txt', 'a')
myFile.write('\nAccessed on ' + param1+str(datetime.now()))
Cron命令
*/1 * * * * cd /Workspace/testcron/ && /Workspace/testcron/venvcron/bin/python3 /Workspace/testcron/testcronwithparam.py param
在上述命令中
*/1 * * * * -每一分钟执行一次
cd /Workspace/testcron/—python脚本所在路径
/Workspace/testcron/venvcron/bin/python3 - Virtualenv路径
Workspace/testcron/testcronwithparam.py -文件路径
参数-参数
从cronfile运行source将不起作用,因为cron使用/bin/sh作为它的默认shell,它不支持source。您需要设置SHELL环境变量为/bin/bash:
SHELL=/bin/bash
*/10 * * * * root source /path/to/virtualenv/bin/activate && /path/to/build/manage.py some_command > /dev/null
很难发现为什么会失败,因为/var/log/syslog没有记录错误细节。最好将自己别名为root,这样你就会收到带有任何cron错误的电子邮件。只需将自己添加到/etc/aliases并运行sendmail -bi。
更多信息:
http://codeinthehole.com/archives/43-Running-django-cronjobs-within-a-virtualenv.html
上面的链接更改为:
https://codeinthehole.com/tips/running-django-cronjobs-within-a-virtualenv/
我很抱歉第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自动。