skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER
    .
    ├── 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
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search