我希望我的自定义Django命令每分钟执行一次。但是,似乎python /path/to/project/myapp/manage.py mycommand
在目录下python manage.py mycommand
工作正常时似乎不起作用。
我该如何实现?我使用/etc/crontab:
****** root python /path/to/project/myapp/manage.py mycommand
堆栈溢出
产品展示
搜索…
登录 注册
家
上市
堆栈溢出
标签
用户数
工作
队伍
这是什么?
30天免费试用
Django自定义命令和cron
问问题
问 10年零5个月前
活跃 2年,1个月前
观看了 13k次
16
14
我希望我的自定义Django命令每分钟执行一次。但是,似乎python /path/to/project/myapp/manage.py mycommand在目录下python manage.py mycommand工作正常时似乎不起作用。
我该如何实现?我使用/etc/crontab:
** root python /path/to/project/myapp/manage.py mycommand
django django-admin cron crontab
跟随分享改善这个问题
10年4月15日于13:55编辑
彼得·莫滕森
25.2千2121金币9090银徽章118个118青铜徽章
问09年10月21日在14:22
海尔纳
44.6千6969金徽章181181银章266266青铜徽章
添加评论
6个答案
活跃的
最古老的
投票
19
我认为问题在于cron将在“裸机”环境中运行脚本,因此您的DJANGO_SETTINGS_MODULE可能未定义。您可能需要将其包装在首先定义DJANGO_SETTINGS_MODULE的shell脚本中
像这样:
export DJANGO_SETTINGS_MODULE=myproject.settings
./manage.py mycommand
使它可执行(chmod + x),然后设置cron来运行脚本。
编辑
我也想说的是,您可以稍微“模块化”这个概念,并使您的脚本接受manage命令作为参数。
export DJANGO_SETTINGS_MODULE=myproject.settings
./manage.py ${*}
现在,您的cron作业可以简单地传递“ mycommand”或您要从cron作业运行的任何其他manage.py命令。
跟随分享改善这个答案
11年9月16日于19:50编辑
09年10月21日在14:32 回答
乔·霍洛威
23.2千1212金徽章7474银色徽章8989枚青铜徽章
1个
这是我执行时说的未知命令。我必须将其更改为:cd / path / to / project ./manage.py $ {*} – mhost 10年 2月21日,19:27
没有成功 可以识别自定义命令,但该命令显然找不到数据库 – Raphael 2011年 9月15日,18:57
您“命令显然找不到数据库”是什么意思?这会带来某种错误吗? – Joe Holloway 2011年9月16日19:48
添加评论
9
如果您希望Django的生活更加简单,请在项目中使用django-command-extensions:
http://code.google.com/p/django-command-extensions/
您将找到一个名为“ runscript”的命令,因此只需将命令添加到crontab行中:
** root python /path/to/project/myapp/manage.py runscript mycommand
这样的脚本将在Django上下文环境中执行。
跟随分享改善这个答案
10年4月15日于13:57编辑
彼得·莫滕森
25.2千2121金币9090银徽章118个118青铜徽章
09年10月21日在14:50 回答
萘丁酚
1,23311金徽章1111银徽章2323枚青铜徽章
添加评论
0
The runscript extension wasn’t well documented. Unlike the django command this one can go anywhere in your project and requires a scripts folder. The .py file requires a run() function.
followshareimprove this answer
answered Apr 11 ‘10 at 0:23
sherwoodiv
1
add a comment
13
cd /path/to/project/myapp && python manage.py mycommand
By chaining your commands like this, python will not be executed unless cd correctly changes the directory.
followshareimprove this answer
answered Apr 11 ‘10 at 0:30
davidfischer
40133 silver badges33 bronze badges
This was the easiest way to do it for me and it worked well. Thanks David! – thomallen Apr 6 ‘11 at 23:55
2
that didn’t work for me (still having a No module named django.core.management error in my mails) – Guillaume Gendre Apr 30 ‘12 at 15:32
add a comment
2
This is what i have done recently in one of my projects,(i maintain venvs for every project i work, so i am assumning you have venvs)
* /path/to/venvs/bin/python /path/to/app/manage.py command_name
This worked perfectly for me.
followshareimprove this answer
answered Jun 18 ‘13 at 10:54
Nitish Kansal
28822 silver badges66 bronze badges
add a comment
0
If its a standalone script, you need to do this:
from django.conf import settings
from django.core.management import setup_environ
setup_environ(settings)
If its a django command, its easier: https://coderwall.com/p/k5p6ag
In (management/commands/exporter.py)
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
args = ‘’
help = ‘Export data to remote server’
def handle(self, *args, **options):
# do something here
And then, in the command line:
$ python manage.py exporter
Now, it’s easy to add a new cron task to a Linux system, using crontab:
$ crontab -e
or $ sudo crontab -e if you need root privileges
In the crontab file, for example for run this command every 15 minutes, something like this:
/15 * * * python /var/www/myapp/manage.py exporter
followshareimprove this answer
answered Aug 15 ‘14 at 19:01
zengr
34.8k3434 gold badges119119 silver badges185185 bronze badges
add a comment
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name
Email
Required, but never shown
Post Your Answer
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you’re looking for? Browse other questions tagged django django-admin cron crontab or ask your own question.
The Overflow Blog
Introducing Dark Mode (beta) for Stack Overflow
Podcast 222: Learning From our Moderators
Featured on Meta
An Update On Creative Commons Licensing
Community and Moderator guidelines for escalating issues via new response…
Triage needs to be fixed urgently, and users need to be notified upon…
Upcoming Feature: New Question Close Experience
Dark Mode Beta - help us root out low-contrast and un-converted bits
Looking for a job?
Senior Python Developer (Remote)
X-TeamNo office location
REMOTE
pythondjango
Engineer, Platform - MongoDB/RDS
Auth0No office location
REMOTE
javapython
Core Java Developer
Flow TradersAmsterdam, Netherlands
€60K - €80KVISA SPONSORSHIP
swinghibernate
Senior Software Engineer, Backend (UK)
NumbrsNo office location
REMOTE
javamachine-learning
Full-Stack Software Engineer
AppInChinaChaoyang Qu, China
$40K - $60KRELOCATION
javasql
Paid Research Study for Productivity Tool Administrators/Managers
User Research InternationalNo office location
REMOTE
adminuser-management
Full-Stack Engineer
Octane AINo office location
REMOTE
pythonjavascript
View more jobs on Stack Overflow
Linked
1
How can I periodically run a Python script to import data into a Django app?
2
Django Management Command Cron on Dotcloud
0
Is it possible to send date triggered email in Django?
3
Could not import Django error after running manage.py runcrons in crontab (MacOs Sierra)
0
Django custom manage command sometimes working, sometimes not, depending on the path
1
Interacting with a program in the background of Django
Related
846
How do I list all cron jobs for all users?
1
Run custom Django management command over SSH
4
Scheduling django custom command
418
Restarting cron after changing crontab file?
0
django cron error on webfaction
11
How to write custom django manage.py commands in multiple apps
4
Cron not running django command
Hot Network Questions
How do I politely decline a professor who’s really keen on having me for a PhD position?
How difficult is it to convert a hypergolic based Moon human lander to a cryogenic one?
How does a disease cause its hosts to travel more?
Quasi-linear PDE equation
Why does apt install php try to install apache
Why do photons follow specific path after reflection from a mirror surface if they can be emitted in any direction by electrons of mirror surface?
Definition of buckling as a sudden deformation
Verb for refusing to share an item
Tikzpicture with caption
How do you say “I have two sons”?
Which measurements are most important to consider for tall men?
Generating generating expressions for sequences
Ending the Levitate spell at a considerable height
How does a telescope make an image larger by shrinking it?
Why is “fhepfcelehfcepfffacacacacacacabn” a top DNS query from my devices?
If liquid and gas are both chaotic states of matter, what’s the difference between them on the molecular level?
70s (or earlier) movie with a monster dinosaur in England
Do any national chess bodies not strictly follow FIDE rules?
What to do when visa expires during lockdown? Malaysia
During an eclipse, how big is the shadow of the moon on the earth?
How can a red light photon be different from a blue light photon?
Is the space inside a demiplane considered “outdoors” for the purposes of Control Weather?
连字符的使用不正确,形式为“艺术家-歌曲名称”
什么设备从地球带到月球表面并返回?
问题提要
堆栈溢出
问题
工作
开发人员工作目录
工资计算器
救命
移动
禁用响应
产品展示
队伍
天赋
广告
企业
公司
关于
按
在这里工作
法律
隐私政策
联系我们
堆栈交换
网络
技术
生活/艺术
文化/娱乐
科学
其他
博客
脸书
推特
领英
网站设计/徽标©2020 Stack Exchange Inc; cc by-sa 4.0许可的用户贡献 ,需要提供署名。 修订版2020.3.31.36413
本文向大家介绍Django自定义manage命令实例代码,包括了Django自定义manage命令实例代码的使用技巧和注意事项,需要的朋友参考一下 manage.py是在我们创建Django项目的时候就自动生成在根目录下的一个命令行工具,它可以执行一些简单的命令,其功能是将Django project放到sys.path目录中,同时设置DJANGO_SETTINGS_MODULE环境变量为当前pr
是否可以使用用户扩展在Selenium IDE中创建自定义命令.js并使其能够将自身更改为自定义导出格式,如果是这样,如何? 我将使用SeleniumIDE记录测试,这些测试将以完全自定义的Java格式导出,作为全自动测试系统的一部分运行。格式不是JUnit,而是每个测试都是它自己的类,并根据整个系统的输入动态加载。 我想要做的是在Selenium IDE中创建一个名为“take snapshot
3.3 自定义命令 命令语法 定义命令与定义映射的用法其实很相似: :command {lhs} {rhs} 只不过在使用自定义命令时,{lhs} 是直接输入到命令行中的,当你按下回车时,vim 就将 {lhs} 替换为 {rhs} 再执行。所以这在形式上与下面这个映射等效: : nnoremap :{lhs}<CR> :{rhs}<CR> 当然,由于 :command 所支持的参数与 :ma
问题内容: 我有一个名为“ example”的库,该库正在安装到我的全局site- packages目录中。但是,我希望能够安装两个版本,一个用于生产版本,一个用于测试版本(我有一个Web应用程序,而其他东西也都以这种方式进行了版本化)。 有没有一种方法可以指定,例如“ python setup.py stage”,它不仅可以将其他鸡蛋安装到站点程序包中,还可以将模块从“ example”重命名为
假设我需要一个新的git命令gitnew,它生成一个新分支,该分支与origin/master是最新的。 是否有一种方法可以使此脚本在powershell的Windows上的所有存储库中都可用? 编辑:为了澄清我想要一个git脚本,而不是一个PowerShell函数。我提到PowerShell的唯一原因是因为我不使用git bash。
应用服务启动可以在多个地方进行调整,以下介绍两种方案,Dockerfile 和 平台详情页调整 Dockerfile CMD启动方式 看一下简单的例子: FROM hub.kpaas.nsini.com/app/hello:v0.0.3 CMD ["/go/bin/hello"] 启动命令写在CMD这个后面,如果后面有多个参数可以以逗号隔开例如: CMD ["static-web", "-pa