Running Shell script to run Django shell and then scp files to server...

438 views
Skip to first unread message

Chris McComas

unread,
Oct 2, 2012, 10:09:54 AM10/2/12
to django...@googlegroups.com
I have a script in Django that I run manually 'python manage.py shell' and then 'from sports import scores' and it goes out and pulls scores from a couple URLs, modifies the data, and saves it to a SQLite database. Once that process runs I manually scp my files to a server...

What I'd like to do is write a shell script that I can run on a cron to run the process at set periods, both actions.

How would I do this so it properly runs the Django shell script?

Tom Evans

unread,
Oct 2, 2012, 10:47:41 AM10/2/12
to django...@googlegroups.com
You can write your own custom command - so run "python manage.py mycommand":

https://docs.djangoproject.com/en/1.4/howto/custom-management-commands/

Or you can setup your environment correctly, eg PYTHONPATH,
DJANGO_SETTINGS_MODULE et al, and run a regular python script. No docs
for this one, and I've always done the former rather than the latter.


Cheers

Tom

Gelonida N

unread,
Oct 2, 2012, 4:40:55 PM10/2/12
to django...@googlegroups.com
If you don't want to write a management command:

This is what you roughly have to do:

import os, import sys
# setup sys.path such, that it finds all required modules

# New setup the environment so, that django settings can be found
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'

# next lines recommended to be sure your own code can use logging
from django.conf import settings

# DO NOT REMOVE NEXT COMMAND (It forces django.setting's
# lazy evaluation to evaluate)
LOGGING = settings.LOGGING

import logging
# Now you can use logging with django setting's log setup

# Now You're free to use django

from your_project.your_app.models import MyModel












>
>
> Cheers
>
> Tom
>


Reply all
Reply to author
Forward
0 new messages