Cron 和 virtualenv [英] Cron and virtualenv

查看:27
本文介绍了Cron 和 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 调用 activate 脚本什么也没做.这有效,以此类推.

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 脚本中进行相同的更改:

Another thing to try is to make the same change in your manage.py script at the very top:

#!/home/my/virtual/bin/python

这篇关于Cron 和 virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆