Adam Thompson
Adam Thompson

Reputation: 3516

Running a Python Script every n Days in Django App

I am looking to run a Python script every n days as part of a Django app.

I need this script to add new rows to the database for each attribute that a user has.

For example a User has many Sites each that have multiple Metric data points. I need to add a new Metric data point for each Site every n days by running a .py script.

I have already written the script itself, but it just works locally.

Is it the right approach to:

1) Get something like Celery or just a simple cron task running to run the python script every n days.

2) Have the python script run through all the Metrics for each Site and add a new data point by executing a SQL command from the python script itself?

Upvotes: 1

Views: 1273

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

The first step is to write the script - which you have already done. This script should run the task which is to be repeated.

The second step is to schedule the execution.

The de-facto way of doing that is to write a cron entry for the script. This will let the system's cron daemon trigger the script at the interval you select.

To do so, create an entry in the crontab file for the user that owns the script; the command to do so is crontab -e, which will load a plain text file in your preferred editor.

Next, you need to add an entry to this file. The format is:

minutes hours day-of-month month day-of-week script-to-execute

So if you want to run your script every 5 days:

0 0 */5 * * /bin/bash /home/user/path/to/python.py 

Upvotes: 2

chyoo CHENG
chyoo CHENG

Reputation: 720

You can use Django's manage commad, then use crontab to run this command circularly.

Upvotes: 2

Related Questions