Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Please #60

Closed
brayan15 opened this issue Oct 14, 2017 · 11 comments
Closed

Help Please #60

brayan15 opened this issue Oct 14, 2017 · 11 comments

Comments

@brayan15
Copy link

I defined my task, but doesn't want show, I added a simple code:
def my_scheduled_job():
#your functionality goes right here
print('hello')

and on setting I putted this:
CRONJOBS = [
('*/2 * * * *', 'vehicle_control_system.persons.contrab.my_scheduled_job'),
]

but first I ran python manage.py contrab add and later I run django with python manage.py runserver

@kraiz
Copy link
Owner

kraiz commented Oct 14, 2017

Ok and what's the problem?
What the output of crontab -l on the console?

@brayan15
Copy link
Author

the output was this:
*/2 * * * * /Users/brayanlara/virtualenvs/vehicle/bin/python /Users/brayanlara/Desktop/Projects/vehicle_control_system/manage.py crontab run b11ab998cc14f55a4642db8bfd678835 # django-cronjobs for config

@addisonlynch
Copy link

vehicle_control_system.persons.contrab -> vehicle_control_system.persons.crontab?

@brayan15
Copy link
Author

brayan15 commented Oct 14, 2017

not, because my file called contrab and als I run python manage.py. crontab add later python manage.py runserver, but never doesn't show print, only show print when I run python manage.py crontab run b11ab998cc14f55a4642db8bfd678835, but with python manage.py runserver never run.

@kraiz
Copy link
Owner

kraiz commented Oct 17, 2017

Sure...as the command will be called from a system service not from within your shell. If you're interested in the stdout stream you should redirect it into a logfile. It's documeted in the README, have a look at example 1 on settting CRONJOBS.

@brayan15
Copy link
Author

ty, I'm going to review

@rollue
Copy link

rollue commented Oct 30, 2017

@kraiz I'm having the same trouble. I CAN add and run with python manage.py crontab add or python manage.py crontab run xxxxxx, but if I do python manage.py runserver to see if django server is actually doing my scheduled jobs(scheduled_crawl to create CrawlLog for testing purposes), nothing happens. I tried redirecting it into logfile like you suggested, but it's not working neither.

settings.py

import os


CRONJOBS = [
    ('* * * * *', 'crawlers.cronjobs.scheduled_crawl', '>> /tmp/crawllog.log'),
]

crawlers.cronjobs.py

from .models import CronLog


def scheduled_crawl():
    CronLog.objects.create()

crawlers.models.py

from django.db import models


class CronLog(models.Model):
    date = models.DateTimeField(auto_now_add=True, blank=True)

    def __unicode__(self):
        return self.date.ctime()

crontab -l

* * * * * /Users/mhjeon/.pyenv/versions/3.6.0/envs/djangocrawler/bin/python /Users/mhjeon/projects/djangocrawler/manage.py crontab run b7143ae3a11fd76146b49a5f54d11706 >> /crawllog.log # django-cronjobs for core

I'm using django within pyenv environment. Any ideas what it is not working?

@rollue
Copy link

rollue commented Nov 3, 2017

@brayan15 @kraiz
I managed to figure out the solution. Apparently, crontab does not automatically pick up local virtual environment's(=pyenv in my case) environment variables. So you have to set them manually using crontab -e command.

After setting the following 4 environment variables in crontab -e, crontab started working as expected. (Not all of the four environment variables may be needed; I'll leave it up to you to test that :))

DJANGO_SETTINGS_MODULE=core.settings.development
DATABASE_URL=postgres://my_id:my_password@localhost:5432/my_db_name
DJANGO_SECRET_KEY=my_django_secret key
PATH=~/.pyenv/shims:~/.pyenv/bin:"$PATH"

*/1 * * * * /Users/mhjeon/.pyenv/versions/3.6.0/envs/djangocrawler/bin/python /Users/mhjeon/projects/djangocrawler/manage.py crontab run b9a4006b4087d6494c1b442bcadb1db0 >> /tmp/scheduled_job.log # django-cronjobs for core

I guess this probably should be added to doc or REAME, for I have spent more than 5 days searching for the right answer in futile. I owe getting the hint about environment variables to this blog, which in Korean.

Note: I'm yet to test this solution on live, on AWS EBS. I only tested on local development settings. But I presume if you set those environment variables correctly, I should work on remote servers also.

@kraiz
Copy link
Owner

kraiz commented Nov 5, 2017

Hi @mhoonjeon, that's kind of strange. django-crontab writes the python interpeter (or virtualenv) used to run the add subcommand into cronjob which seems to work fine for you.

It seems pyenv really need this PATH changes to actually work. Can you please assist and remove just the PATH line. If it's not working then, we double checked it and i'll add a pyenv-hint into the FAQ section of the README.

@kraiz kraiz closed this as completed in 1c5a0ec Nov 26, 2017
@clock21am
Copy link

can you tell me what is the exactly that path is point to...I am getting the similiar issue

@chirag-jn
Copy link

I usually add the following variables in crontab -e and things seem to be moving:

SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants