一尘不染

Django自定义命令和cron

django

我希望我的自定义Django命令每分钟执行一次。但是,似乎python /path/to/project/myapp/manage.py mycommand在目录下python manage.py mycommand工作正常时似乎不起作用。

我该如何实现?我使用/etc/crontab:

****** root python /path/to/project/myapp/manage.py mycommand

阅读 399

收藏
2020-03-31

共1个答案

一尘不染

堆栈溢出
产品展示
搜索…
登录 注册

上市
堆栈溢出
标签
用户数
工作
队伍
这是什么?
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脚本中

像这样:

!/bin/bash

export DJANGO_SETTINGS_MODULE=myproject.settings
./manage.py mycommand
使它可执行(chmod + x),然后设置cron来运行脚本。

编辑

我也想说的是,您可以稍微“模块化”这个概念,并使您的脚本接受manage命令作为参数。

!/bin/bash

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)

your code here which uses django code, like django model

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:

m h dom mon dow command

/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

2020-03-31