0

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.

Brian Destura
  • 11,487
  • 3
  • 18
  • 34
Saran Prasad
  • 125
  • 2
  • 13
  • Did you mean you are not seeing the print? I don't think it will display on whichever terminal you are on. Try to check if the file is updated every minute – Brian Destura Aug 02 '21 at 04:32
  • Yes I checked file, i am keeping a file for that. File only get updated whenever i called python manage.py crontab run.. Not on runserver. – Saran Prasad Aug 02 '21 at 05:21
  • It should be run every minute regardless if you used runserver, because cron will run it for you. Just to confirm, what OS are you using? – Brian Destura Aug 02 '21 at 05:24
  • I'm using Ubuntu 20.04. – Saran Prasad Aug 02 '21 at 05:26
  • You did run `python manage.py crontab add` right? Try to see if your cron job was added on your crontab. Check [this](https://unix.stackexchange.com/a/7060) on how. If it is there, that job will run every minute even without `runserver` or `crontab run` – Brian Destura Aug 02 '21 at 05:28
  • yees..when i type crontab -l, it show my job with id. – Saran Prasad Aug 02 '21 at 05:33
  • Then that should be fine. It should run every minute – Brian Destura Aug 02 '21 at 05:35
  • But not running in my case, i have used timezone.now() to check whether its updating or not. but its not coming as excepted. only get update when i running python manage.py crontab run. i have a doubt is user permission anything to do with it. whether user have permission to run cronjob. – Saran Prasad Aug 02 '21 at 05:49
  • That's possible. Because `python manage.py crontab run` runs the command directly from django. – Brian Destura Aug 02 '21 at 05:51
  • okay. let me check that scenario. – Saran Prasad Aug 02 '21 at 05:58

1 Answers1

0

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.