Django custom command and cron

后端 未结 6 1840
刺人心
刺人心 2020-12-02 15:17

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

6条回答
  •  借酒劲吻你
    2020-12-02 15:30

    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

    Something like this:

    #!/bin/bash
    
    export DJANGO_SETTINGS_MODULE=myproject.settings 
    ./manage.py mycommand
    

    Make it executable (chmod +x) and then set up cron to run the script instead.

    Edit

    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 ${*}
    

    Now, your cron job can simply pass "mycommand" or any other manage.py command you want to run from a cron job.

提交回复
热议问题