如何在crontab + virtual env中运行自定义manage.py?

时间:2021-03-04 00:54:03

How to run in crontab

如何在crontab中运行

*/1 * * * * /home/user/Desktop/job/dp/ python manage.py statistics

with virtual env? I need to activate virtualenv first(Otherwise it does not work)

用虚拟环境?我需要先激活virtualenv(否则它不起作用)

This is my virtual env:

这是我的虚拟环境:

source job/bin/activate

2 个解决方案

#1


13  

EDITED:

Try something like this:

尝试这样的事情:

*/1 * * * * . /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics

This should be read as: activate the env and if that was successful, excute the manage.py script. Since manage.py is supposed to have a python shebang and the virtual env sets the correct python interpreter, this should work.

这应该被理解为:激活env,如果成功,则执行manage.py脚本。由于manage.py应该有一个python shebang而虚拟env设置正确的python解释器,这应该可行。

Apparently cron usually runs with /bin/sh which does not know the source command. So one option is to use a dot as a source replacement. Another to set /bin/bash in the cron file:

显然cron通常使用/ bin / sh运行,它不知道源命令。因此,一种选择是使用点作为源替换。另一个在cron文件中设置/ bin / bash:

SHELL=/bin/bash
*/1 * * * * source /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics

Read more about this issue at: http://codeinthehole.com/writing/running-django-cronjobs-within-a-virtualenv/ The article doesn't mention that source can be replaced by a ., but i've just tried it and it worked for me. So you have several options to choose from now, the article even has others. ;)

有关此问题的更多信息,请访问:http://codeinthehole.com/writing/running-django-cronjobs-within-a-virtualenv/文章未提及源代码可以替换为。,但我刚试过它对我有用。所以你现在有几个选择可供选择,文章甚至还有其他选择。 ;)

#2


4  

Use something like ~/envs/someenv/lib/python /path/to/your/script

使用〜/ envs / someenv / lib / python / path / to / your / script之类的东西

In your situation it will look like

在你的情况下,它看起来像

*/1 * * * * ~/envs/someenv/lib/python /home/user/Desktop/job/dp/manage.py statistics

* / 1 * * * *〜/ envs / someenv / lib / python /home/user/Desktop/job/dp/manage.py statistics

#1


13  

EDITED:

Try something like this:

尝试这样的事情:

*/1 * * * * . /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics

This should be read as: activate the env and if that was successful, excute the manage.py script. Since manage.py is supposed to have a python shebang and the virtual env sets the correct python interpreter, this should work.

这应该被理解为:激活env,如果成功,则执行manage.py脚本。由于manage.py应该有一个python shebang而虚拟env设置正确的python解释器,这应该可行。

Apparently cron usually runs with /bin/sh which does not know the source command. So one option is to use a dot as a source replacement. Another to set /bin/bash in the cron file:

显然cron通常使用/ bin / sh运行,它不知道源命令。因此,一种选择是使用点作为源替换。另一个在cron文件中设置/ bin / bash:

SHELL=/bin/bash
*/1 * * * * source /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics

Read more about this issue at: http://codeinthehole.com/writing/running-django-cronjobs-within-a-virtualenv/ The article doesn't mention that source can be replaced by a ., but i've just tried it and it worked for me. So you have several options to choose from now, the article even has others. ;)

有关此问题的更多信息,请访问:http://codeinthehole.com/writing/running-django-cronjobs-within-a-virtualenv/文章未提及源代码可以替换为。,但我刚试过它对我有用。所以你现在有几个选择可供选择,文章甚至还有其他选择。 ;)

#2


4  

Use something like ~/envs/someenv/lib/python /path/to/your/script

使用〜/ envs / someenv / lib / python / path / to / your / script之类的东西

In your situation it will look like

在你的情况下,它看起来像

*/1 * * * * ~/envs/someenv/lib/python /home/user/Desktop/job/dp/manage.py statistics

* / 1 * * * *〜/ envs / someenv / lib / python /home/user/Desktop/job/dp/manage.py statistics