djangopython-3.xcrondjango-cron

How to hook a debugger to a python code running via django crontab?


I have a Django based web application, some functionality of the application is scheduled to be run as a part of cron jobs using django-crontab. I want to hook a debugger so that I can inspect some odd behaviours of my code. I normally use visual studio code. Is it possible to hook a debugger, since cron jobs basically run independently apart from server?


Solution

  • You can put a breaking point debugger in the code using pdb or ipdb. Like this:

    def some_function():
        # some code
        import pdb;pdb.set_trace()  # or use ipdb
        # rest of the code
    

    Then in shell, run python manage.py crontab show to show cronjobs with ids, then run python manage.py crontab run <id>. It will hit the debugger, then you will hit the breaking point. Thus you can use debugger here.