0

hello guys i trying to use django_crontab on my django project and there not working does anyone know something about this im using Linux centos 8. I want to schedule a task to add some data to my database. Can someone help me

The steps that i have take is:

  1. pip install django-crontab
  2. add to the installed apps
  3. build my cron function

` from django.core.management.base import BaseCommand from backups.models import Backups from devices.models import Devices from datetime import datetime from jnpr.junos import Device from jnpr.junos.exception import ConnectError from lxml import etree from django.http import HttpResponse from django.core.files import File

class Command(BaseCommand): def handle(self, *args, **kwargs): devices = Devices.objects.all()

    for x in devices:
        devid = Devices.objects.get(pk=x.id)
        ip = x.ip_address
        username = x.username
        password = x.password

        print(devid, ip, username, password)
        dev1 = Device(host= ip ,user= username, passwd= password)
        try:
            dev1.open()
            stype = "sucsess" 

            
        
            dataset = dev1.rpc.get_config(options={'format':'set'})
            datatext = dev1.rpc.get_config(options={'format':'text'})

            result =  (etree.tostring(dataset, encoding='unicode'))

            file_name = f'{ip}_{datetime.now().date()}.txt'
            print(file_name)
            with open("media/"f'{file_name}','w') as f:
                f.write(etree.tostring(dataset, encoding='unicode'))
                f.write(etree.tostring(datatext, encoding='unicode'))
            
                
        

            backup = Backups(device_id=devid, host=ip, savetype=stype, time=datetime.now(), backuptext=file_name)
            print(backup)
            backup.save()
        except ConnectError as err:
            print ("Cannot connect to device: {0}".format(err))
            print("----- Faild ----------")
            stype = ("Cannot connect to device: {0}".format(err))
            backup = Backups(device_id=devid, host=ip, savetype=stype, time=datetime.now())
            backup.save()
`
  1. add my cronjob to my setting.py file :

CRONJOBS = [ ('*/5 * * * *', 'django.core.management.call_command', ['backup-dev']), ]

5)

python manage.py crontab add

6)

python manage.py crontab show

Currently active jobs in crontab: 0662c1224789b131740fddef54f273c1 -> ('* * * * *', 'django.core.management.call_command', ['backup-dev'])

and still not working any ideas

and when i run this command: " python manage.py backup-dev" my task working perfectly

i Also try to add the management command direct to the centos machine via crontab with the command

crontab -e

and still nothing any ideas

0 Answers0