Setting up cron jobs for regular tasks

Usually websites have some management tasks to do in the background once in a week, day, or every hour. This can be achieved using cron jobs that are also known as scheduled tasks. These are scripts that run on the server for the specified period of time. In this recipe, we will create two cron jobs: one to clear sessions from the database and another to back up the database data. Both will be run every night.

Getting ready

To start with, deploy your Django project on to a remote server. Then, connect to the server by SSH.

How to do it...

Let's create the two scripts and make them run regularly by following these steps:

  1. Create the commands, db_backups and logs directories in your project's home directory:
    (myproject)myproject@server$ ...

Get Django: Web Development with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.