4
8 Comments

Cron jobs on Django?

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.

Trending on Indie Hackers
How I grew a side project to 100k Unique Visitors in 7 days with 0 audience 47 comments Competing with Product Hunt: a month later 33 comments Why do you hate marketing? 28 comments $15k revenues in <4 months as a solopreneur 14 comments Use Your Product 13 comments How I Launched FrontendEase 13 comments