Django crontab不执行测试功能 [英] Django crontab not executing test function

查看:46
本文介绍了Django crontab不执行测试功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

  python manage.py crontab show # this command give following

  Currently active jobs in crontab:
  12151a7f59f3f0be816fa30b31e7cc4d -> ('*/1 * * * *', 'media_api_server.cron.cronSendEmail')

  • 我的应用在虚拟环境(env)中处于活动状态

  • My app is in virtual environment (env) active

    在我的media_api_server/cron.py中,我具有以下功能:

    In my media_api_server/cron.py I have following function:

    def cronSendEmail():
        print("Hello")
        return true 
    

  • 在settings.py模块中:

  • In settings.py module:

    INSTALLED_APPS = (
        ......
        'django_crontab',
    )
    
    CRONJOBS = [
        ('*/1 * * * *', 'media_api_server.cron.cronSendEmail')
    ]
    

  • 对我来说,所有定义就位,但是当我在虚拟环境中运行python manage.py runserver时,控制台不会打印任何内容.

    To me all defined in place but when I run python manage.py runserver in virtual environment, console doesn't print any thing.

        System check identified no issues (0 silenced).
        July 26, 2016 - 12:12:52
        Django version 1.8.1, using settings 'mediaserver.settings'
        Starting development server at http://127.0.0.1:8000/
        Quit the server with CONTROL-C.
    

    'Django-crontab'模块不起作用,我在这里遵循了其文档 https://pypi.python.org/pypi/django-crontab

    'Django-crontab' module is not working I followed its documentation here https://pypi.python.org/pypi/django-crontab

    推荐答案

    您的代码确实有效.您可能会认为 print("Hello")应该出现在stdout中?因此,这种方式行不通,因为cron的输出没有使用 stdour stderr .要查看实际结果,您应该在 CRONJOBS 列表中指向某些日志文件的路径:只需将'>>/path/to/log/file.log'作为最后一个参数,例如:

    Your code actually works. You may be think that print("Hello") should appear in stdout? So it doesn't work that way, because cron doesn't use stdour and stderr for it's output. To see actual results you should point path to some log file in CRONJOBS list: just put '>> /path/to/log/file.log' as last argument, e.g:

    CRONJOBS = [
        ('*/1 * * * *', 'media_api_server.cron.cronSendEmail', '>> /path/to/log/file.log')
    ]
    

    同样,将您的错误也重定向到stdout可能会有所帮助.为此,您需要在 settings.py

    Also it might be helpful to redirect your errors to stdout too. For this you heed to add CRONTAB_COMMAND_SUFFIX = '2>&1' to your settings.py

    这篇关于Django crontab不执行测试功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆