Django 自定义命令和 cron [英] Django custom command and cron
问题描述
我希望我的自定义 Django 命令每分钟执行一次.然而,python/path/to/project/myapp/manage.py mycommand
似乎在 python manage.py mycommand
目录下似乎不起作用.
I want my custom made Django command to be executed every minute. However it seems like python /path/to/project/myapp/manage.py mycommand
doesn't seem to work while at the directory python manage.py mycommand
works perfectly.
我怎样才能做到这一点?我使用 /etc/crontab
与:
How can I achieve this ? I use /etc/crontab
with:
****** root python /path/to/project/myapp/manage.py mycommand
推荐答案
我认为问题在于 cron 将在裸"环境中运行您的脚本,因此您的 DJANGO_SETTINGS_MODULE 可能未定义.您可能希望将其包装在首先定义 DJANGO_SETTINGS_MODULE 的 shell 脚本中
I think the problem is that cron is going to run your scripts in a "bare" environment, so your DJANGO_SETTINGS_MODULE is likely undefined. You may want to wrap this up in a shell script that first defines DJANGO_SETTINGS_MODULE
像这样:
#!/bin/bash
export DJANGO_SETTINGS_MODULE=myproject.settings
./manage.py mycommand
使其可执行(chmod +x),然后设置 cron 来运行脚本.
Make it executable (chmod +x) and then set up cron to run the script instead.
编辑
我还想说,您可以稍微模块化"这个概念,并使其脚本接受管理命令作为参数.
I also wanted to say that you can "modularize" this concept a little bit and make it such that your script accepts the manage commands as arguments.
#!/bin/bash
export DJANGO_SETTINGS_MODULE=myproject.settings
./manage.py ${*}
现在,您的 cron 作业可以简单地传递mycommand"或您想要从 cron 作业运行的任何其他 manage.py 命令.
Now, your cron job can simply pass "mycommand" or any other manage.py command you want to run from a cron job.
这篇关于Django 自定义命令和 cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!