Is there any another way to python manage.py crontab run in django server?

1.5k Views Asked by At

i have created a scheduled job in django with django_crontab package. i have added the job. its successfully added but its only get executed whenever i run python manage.py crontab run daeea9e88c171494b1610bdebfasd123 not with runserver command. what might be the issue, this is my view.

cron.py file

def my_scheduled_job():
    file = open('geek.txt','w')
    now = timezone.now()
    file.write("This is the write command")
    file.write("It allows us to write in a particular file")
    file.write(str(now))
    file.close()
    print(now)
    return True

settings.py

CRONTAB_LOCK_JOBS = False
CRONTAB_DJANGO_PROJECT_NAME = 'api' 
CRONTAB_DJANGO_SETTINGS_MODULE = 'api.settings'
CRONJOBS = [
    ('*/1 * * * *', 'app.cron.my_scheduled_job')
]

what i want is its job get executed every minute whenever i run python manage.py runserver. Is there anything missing please help me out.

1

There are 1 best solutions below

0
On

I also have the same problem which is solved now. I just need to set my user environ var in the .env file and the issue is solved.

My virtual env path :/home/nasir/Work//dj_venv/bin/python3.8

My cronjob run path : path/home/nasir/Work/django_projects_neosoft/e_commerce/manage.py crontab run 6ef9e6b4bdce09989e8ed084d2ffc2bb

My full path (for my system) : /home/nasir/Work/django_projects_neosoft/dj_venv/bin/python3.8 /home/nasir/Work/django_projects_neosoft/e_commerce/manage.py crontab run 6ef9e6b4bdce09989e8ed084d2ffc2bb

I paste the full path in the terminal and get errors related to the database and environ variable I set environ variables in the .env file and the issue is fixed. cronjob is working fine now.

in your case, u may get some other errors, try to resolve it, at least u know where is the problem.