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很可能未定义。你可能想把它放在一个shell脚本中,首先定义DJANGO_SETTINGS_MODULE
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 或任何其他manage.py命令要从cron作业运行。
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屋!