-1

I'm new to cronjob. I want to retrieve list of data in directory and save the information into my models.

This is my code:

from os.path import getsize
import time
current_user = request.user

root = "/mnt/data/dashboard/"
for path, subdirs, files in os.walk(root):
    for name in files:
        file_name =  name
        size =  getsize(root)
        filepath = path 
        datastr = os.path.join(filepath,name)
        files = datastr.replace( "/mnt/data/dashboard/","" )
        date =  time.ctime(os.path.getctime(root))

        # insert = File(file=files, owner=current_user, _file_size=size, uploaded_at=date, original_filename=file_name, folder_id=6)
        # insert.save()  

        try:
           insert = File.objects.filter(original_filename = file_name)
        except ObjectDoesNotExist:
           insert = File(file=files, owner=current_user, _file_size=size, uploaded_at=date, original_filename=file_name, folder_id=6)
           insert.save() 

What should i do to make sure the script above was execute daily every morning. How to set up cronjob / task scheduler?

Iam Moon
  • 685
  • 2
  • 7
  • 11

1 Answers1

1

For a task like this, I'd advise you to create a Django Management Command

To add it as a daily cronjob. Open your crontab using:

# crontab -e

and add the command using a string like the following:

@daily /path/to/manage.py <name_of_management_command>
BakaKuna
  • 585
  • 4
  • 13