1

I'm trying to start simple crontab-django job scheduled (os is Ubuntu 20.04):

this is the myapp/cron.py file as mentioned in the documentation cron.py

from .models import Cats

    def my_scheduled_job():
      Cats.objects.create(text='Testt')

and this is the settings i used frm the documentation

CRONJOBS = [
    ('*/1 * * * *', 'coins.cron.my_scheduled_job')
]
INSTALLED_APPS = (
    'django_crontab',
    ...
)

i keep getting this error

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django_crontab/management/commands/crontab.py", line 29, in handle
    Crontab().run_job(options['jobhash'])
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django_crontab/crontab.py", line 126, in run_job
    job = self.__get_job_by_hash(job_hash)
  File "/home/madahsm/python projects/corntab/lib/python3.8/site-packages/django_crontab/crontab.py", line 171, in __get_job_by_hash
    raise RuntimeError(
RuntimeError: No job with hash None found. It seems the crontab is out of sync with your settings.CRONJOBS. Run "python manage.py crontab add" again to resolve this issue!

even i tried to add python manage.py crontab add again and show and it appears python manage.py crontab add

removing cronjob: (crontab) -> ('*/1 * * * *', 'coins.cron.my_scheduled_job')
  adding cronjob: (75d02c7cc7be0475f399b3786aefe170) -> ('*/1 * * * *', 'coins.cron.my_scheduled_job')

python manage.py crontab show

crontab -> ('*/1 * * * *', 'coins.cron.my_scheduled_job')

its only work for one time if i run like this:

python manage.py crontab run 75d02c7cc7be0475f399b3786aefe170
DialFrost
  • 1,610
  • 1
  • 8
  • 28
  • Can you share the structure of your app? – sikaili99 Aug 24 '22 at 09:16
  • ├── admin.py ├── apps.py ├── cron.py ├── __init__.py ├── migrations │   ├── 0001_initial.py │   ├── 0002_cats.py │   ├── __init__.py │   └── __pycache__ │   ├── 0001_initial.cpython-38.pyc │   ├── 0002_cats.cpython-38.pyc │   └── __init__.cpython-38.pyc ├── models.py ├── __pycache__ │   ├── admin.cpython-38.pyc │   ├── apps.cpython-38.pyc │   ├── cron.cpython-38.pyc │   ├── __init__.cpython-38.pyc │   └── models.cpython-38.pyc ├── tests.py └── views.py – Ahmad Hashim Aug 24 '22 at 09:58

2 Answers2

0
.
├── coins
│   ├── admin.py
│   ├── apps.py
│   ├── cron.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_cats.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-38.pyc
│   │       ├── 0002_cats.cpython-38.pyc
│   │       └── __init__.cpython-38.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-38.pyc
│   │   ├── apps.cpython-38.pyc
│   │   ├── cron.cpython-38.pyc
│   │   ├── __init__.cpython-38.pyc
│   │   └── models.cpython-38.pyc
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── project
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── settings.cpython-38.pyc
│   │   ├── urls.cpython-38.pyc
│   │   └── wsgi.cpython-38.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── __pycache__
    └── manage.cpython-38.pyc
0

It should already be added to your system crontab. Check it with crontab -l in the terminal.

Edit: It seems because you have a space in your directory name, try to rename python projects to python-projects and rerun the add command.

Đào Minh Hạt
  • 2,742
  • 16
  • 20
  • crontab -l */1 * * * * /home/madahsm/python projects/corntab/bin/python /home/madahsm/python projects/corntab/src/manage.py crontab run 75d02c7cc7be0475f399b3786aefe170 # django-cronjobs for project – Ahmad Hashim Aug 24 '22 at 10:18
  • ├── activate ├── activate.csh ├── activate.fish ├── Activate.ps1 ├── django-admin ├── easy_install ├── easy_install-3.8 ├── pip ├── pip3 ├── pip3.8 ├── python -> python3 ├── python3 -> /usr/bin/python3 ├── sqlformat └── wheel – Ahmad Hashim Aug 24 '22 at 10:19
  • @AhmadHashim it means that it alr registered your job successfully to crontab and will run for every minute check the output of that command `/home/madahsm/python projects/corntab/bin/python /home/madahsm/python projects/corntab/src/manage.py crontab run 75d02c7cc7be0475f399b3786aefe170` or database to see if Cat objects alr created – Đào Minh Hạt Aug 24 '22 at 10:22
  • it only create it if i run it with python manage.py crontab run 75d02c7cc7be0475f399b3786aefe170 – Ahmad Hashim Aug 24 '22 at 10:35
  • but if i run the server it doesn't work – Ahmad Hashim Aug 24 '22 at 10:36
  • @AhmadHashim try the long command above what is the output of `/home/madahsm/python projects/corntab/bin/python /home/madahsm/python projects/corntab/src/manage.py crontab run 75d02c7cc7be0475f399b3786aefe170` ? – Đào Minh Hạt Aug 24 '22 at 10:36
  • it seems because you have a space in your directory name, try to rename `python projects` to `python-projects` and rerun the `add` command. – Đào Minh Hạt Aug 24 '22 at 10:38
  • @AhmadHashim please select my answer LOL – Đào Minh Hạt Aug 24 '22 at 10:49
  • 1
    Sorry still new here dont have enough reputation so sorry brother – Ahmad Hashim Aug 24 '22 at 10:58
  • now i learn how to mark it thanks bro xD – Ahmad Hashim Sep 29 '22 at 18:26