Developers December 20, 2019

Cron jobs on Django?

Landon Roddenberry @LandonR

Hey guys,

I really appreciated all the help in deciding which python framework to use for my app. I ended up going with django and it's slowly coming together. So thank you 🙏.

I've got another question now -> I already have a python script that loops continuously and runs every 24 hours that was built prior to the web app. I'm wondering if I can integrate it on the django platform and have it run on a server (when I deploy) like it currently runs on my PC. (given all the proper dependencies of course).

Again, I'm still a kid at this stuff so I apologise if this makes no sense.

  1. 4

    You can create a Django Management Command that you run from a cronjob.

    On top of that it’s also worth to mention that Django works very well with Celery. You can use Celery to execute asynchronous tasks but also to schedule tasks to be executed on a crontab schedule (but it’s using Celery workers instead of a Linux cronjob under the hood).

    1. 1

      +1 for Celery, or if you don't have a lot of tasks and they aren't very complex, you could also use: https://github.com/arteria/django-background-tasks

      I've used them both and they did a good job, it depends a lot on your use case.

      1. 1

        Hey Mike, I’m trying to use the Django-background-tasks tot run a script every 24 hrs. But I’m not sure I’m doing it right... do you know of a good tutorial?

        1. 2

          I've got my sript working just by reading their documentation: https://django-background-tasks.readthedocs.io/en/latest/

          You just have to use the decoration: @background(schedule=30), this will be run in 30 minutes.

          Of course, when you call such a task, you can change the default schedule like this: function_name(args, schedule={date and time when the task shuld run})

          Let me know if you have other questions.

          1. 1

            Awesome, I’ll probably hit you up again when I deploy the app🙏

    2. 1

      Okay, that’s super helpful! I’ll have to do so research on Celery. I appreciate the help 👊

  2. 3

    Hey, congrats on moving the script to cloud.

    Yeah, CRONS are absolutely fine and easy to start. I have been using Django with crons for last 7 years.

    Few tips:

    Add MAILTO="[email protected]" in crontab to mail the results output of your script. I try to ensure that all my cron scripts have no verbosity unless there is error or warning.

    You can add recurring cron jobs using */n * * * * syntax. Example */5 * * * * will run every 5 minutes.

    Other tips: https://www.endpoint.com/blog/2008/12/08/best-practices-for-cron

    Syntax generator: https://crontab.guru

    Use Cronitor or DeadManSnitch for monitoring very critical cron jobs. Emails for errors have worked fine for me.

    1. 1

      Thank you for the help man! I really appreciate that 🙏 have you ever used cronhub?

      1. 1

        Haven't tried cronhub but it looks good.