Giter Site home page Giter Site logo

django-crontab's Introduction

about

dead simple crontab powered job scheduling for django (1.8-2.0).

setup

install via pip:

pip install django-crontab

add it to installed apps in django settings.py:

INSTALLED_APPS = (
    'django_crontab',
    ...
)

now create a new method that should be executed by cron every 5 minutes, f.e. in myapp/cron.py:

def my_scheduled_job():
  pass

now add this to your settings.py:

CRONJOBS = [
    ('*/5 * * * *', 'myapp.cron.my_scheduled_job')
]

you can also define positional and keyword arguments which let you call django management commands:

CRONJOBS = [
    ('*/5 * * * *', 'myapp.cron.other_scheduled_job', ['arg1', 'arg2'], {'verbose': 0}),
    ('0   4 * * *', 'django.core.management.call_command', ['clearsessions']),
]

finally, run this command to add all defined jobs from CRONJOBS to crontab (of the user which you are running this command with):

python manage.py crontab add

show current active jobs of this project:

python manage.py crontab show

removing all defined jobs is straightforward:

python manage.py crontab remove

config

there are a bunch of setting vars to customize behavior. each of them comes with default values that should properly fit. if not, feel free to overwrite.

CRONJOBS
  • list of jobs, each defined as a tuple:

    • format 1:
      1. required: cron timing in usual format (see Wikipedia and crontab.guru for more examples)
      2. required: the python module path to the method
      3. optional: a job-specific suffix (f.e. to redirect out/err to a file, default: '')
    • format 2:
      1. required: cron timing
      2. required: the python module path to the method
      3. optional: list of positional arguments for the method (default: [])
      4. optional: dict of keyword arguments for the method (default: {})
      5. optional: a job specific suffix (f.e. to redirect out/err to a file, default: '')
  • NOTE: Run "python manage.py crontab add" each time you change CRONJOBS in any way!

  • default: []

  • example:

    CRONJOBS = [
        ('*/5 * * * *', 'myapp.cron.my_scheduled_job'),
    
        # format 1
        ('0   0 1 * *', 'myapp.cron.my_scheduled_job', '>> /tmp/scheduled_job.log'),
    
        # format 2
        ('0   0 1 * *', 'myapp.cron.other_scheduled_job', ['myapp']),
        ('0   0 * * 0', 'django.core.management.call_command', ['dumpdata', 'auth'], {'indent': 4}, '> /home/john/backups/last_sunday_auth_backup.json'),
    ]
CRONTAB_LOCK_JOBS
  • prevent starting a job if an old instance of the same job is still running
  • default: False
  • since 0.5.0
CRONTAB_EXECUTABLE
  • path to the crontab executable of your os
  • default: '/usr/bin/crontab'
CRONTAB_DJANGO_PROJECT_NAME
  • the name of your django project, used to build path path to manage.py and to mark the jobs in crontab via comment for later removing
  • default is read from DJANGO_SETTINGS_MODULE environment variable
CRONTAB_DJANGO_MANAGE_PATH
  • path to manage.py file (including the manage.py itself, i.e. '/home/john/web/manage.py')
  • default is build using DJANGO_PROJECT_NAME
CRONTAB_DJANGO_SETTINGS_MODULE
  • dotted python path to the settings module to run the command with
  • default is the common one from the environment variable and will not be overwritten
  • since 0.6.0
CRONTAB_PYTHON_EXECUTABLE
  • path to the python interpreter executable used to run the scheduled job
  • default uses the interpreter executable used to add the jobs (via 'python manage.py crontab add')
CRONTAB_COMMAND_PREFIX
  • something you want to do or declare, before each job gets executed. A good place for environment variables.
  • default: '' (empty string)
  • example: 'STAGE=production'
CRONTAB_COMMAND_SUFFIX
  • something you want to do after each job was executed.
  • default: '' (empty string)
  • example: '2>&1'
CRONTAB_COMMENT
  • used for marking the added contab-lines for removing, default value includes project name to distinguish multiple projects on the same host and user
  • default: 'django-crontabs for ' + CRONTAB_DJANGO_PROJECT_NAME

contributors

arski cinghiale meric426 justdoit0823 chamaken

faq

  • I'm using this old django version (<1.8) and can't install this package. What should i do? - Yeah, update django of course (!) or - as you seem to be familiar with old, unsupported versions, install the old version of this package too (it support django 1.3-1.7):

    pip install django-crontab==0.6.0
    
  • Will it work with windows? - No.

  • Will it work within a docker? - Not immediately, you need to start the cron service.

  • Problems with `pyenv`? - You maybe need to setup the PATH variable within crontab. Have a look at #60

  • I'm getting "bad command"/"errors in cronfile" while installing via "crontab add". What's wrong? - Maybe it's your cron time format, it can have 5 or 6 fields. Check that your system supports 6 or just define 5 in CRONJOBS. (see #23)

  • Why does the LOGGING not work when started via cronjob? - That's maybe something about the current working dir. Please set your FileHandler's file path absolute and try again. (see #31)

license

MIT-License, see LICENSE file.

django-crontab's People

Contributors

arski avatar bashu avatar dvd0101 avatar jiangmf avatar kraiz avatar meric426 avatar nodiel avatar null-operator avatar petersobhi avatar stardust85 avatar vinsia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-crontab's Issues

error create jobs for multiple sites in one server

i have two apps in same server: /home/django/app1 /home/django/app2
if i'll run python manage.py crontab add in app1-folder it will work
but if i'll run python manage.py crontab add in app2-folder after previous step, it will print error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django_crontab/management/commands/crontab.py", line 20, in handle
    crontab.remove_jobs()
  File "/usr/local/lib/python2.7/dist-packages/django_crontab/crontab.py", line 118, in remove_jobs
    self.__get_job_by_hash(job[0][2][job[0][2].find('crontab run') + 12:].split()[0])
  File "/usr/local/lib/python2.7/dist-packages/django_crontab/crontab.py", line 173, in __get_job_by_hash
    'Run "python manage.py crontab add" again to resolve this issue!' % job_hash
RuntimeError: No job with hash 6e36fac415c5f7df8cec638e7cfb9f5f found. It seems the crontab is out of sync with your settings.CRONJOBS. Run "python manage.py crontab add" again to resolve this issue!

django.core.exceptions.ImproperlyConfigured

Hello,
i'm trying to add and run a task but i've problem having it configured.
I've an enviroment and different confs that change from machine to machine.
When it runs on the server the DJANGO_SETTING_MODULE is set by the wsgi.
So, for the crontab-django i added this:

CRONTAB_DJANGO_SETTINGS_MODULE = 'settings.ste'

i've this job
('* * * * *', 'rest.cron.test', ">> /tmp/out.txt")

def test():
    print "echo"

with turns out to be

* * * * * /Users/stefano/.virtualenvs/myv/bin/python /Users/stefano/SW/aoo-rest/dj/manage.py crontab run 524562a16a9a3585f2cf9511e5cb303d --settings=settings.ste >> /tmp/out.txt # django-cronjobs for settings

if i run django-admin crontab run 524562a16a9a3585f2cf9511e5cb303d it works (but prints the output in the console)
if i paste this in the console it works (prints in the console)
it does not work when executed by cron*
in the /var/mail/stefano i find this every minute

Traceback (most recent call last):
  File "/Users/stefano/SW/rest/dj/manage.py", line 7, in <module>
    django.setup()
  File "/Users/stefano/.virtualenvs/rest/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/stefano/.virtualenvs/rest/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/Users/stefano/.virtualenvs/rest/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Syntax for different scheduled jobs

I have succesfully scheduled jobs that run at the start of every minute and every 5 minutes. This was accomplished by using the examples in the readme. However, when I wanted to schedule a task for the start of every hour I could not register it with cron syntax and when I added a forward slash to it it would register but NOT run.

It would be of great help if you could add these to the examples:

Run task every hour
Run task every week
Run task at midnight
Run task monthly
Run task every year
Run task on 1 st of month
Run task on last day of month

I have searched a lot on the internet to achieve my goal but there is no help regarding this repository.

Thanks in advance.

Regard,
Waleed

Cron doesn't work?

Environment & Versions

  • Operating system: Linux
  • Python: 3.6.1
  • Django: 1.11.4
  • django-crontab: 0.7.1

Settings

  • My django-crontab settings:
CRONJOBS = [('* * * * *', 'core.enquetes.cron.func')]

Details

/usr/src/app# python manage.py crontab show Currently active jobs in crontab: cde74bdd655f575c043ed27d9c7da793 -> ('* * * * *', 'core.enquetes.cron.func')

crontab -l
* * * * * /usr/local/bin/python3 /usr/src/app/manage.py crontab run cde74bdd655f575c043ed27d9c7da793 # django-cronjobs for base

python manage.py crontab run cde74bdd655f575c043ed27d9c7da793 (inside docker container) - WORK

But the automatic version dosent work ;/

Logging jobs altogether

If you have two jobs that are running in parallel at the same time, and both of them want to write to same log file, only one of the will succeed. But in most cases you don't want to separate log file for each specific job and you want to log them altogether. I think currently django-crontab does not handle this situation, does it?

Using with virtualenv?

Hi there!
I'm wanting to use Django-Crontab with virtualenv and am running into some issues.

If I give the full package path (I.e., "myproject.myapp.cron.myjob"), I get the following error:

No module named myapp.cron

If I change the path to myapp.cron.myjob, I get the following error:

No handlers could be found for logger 'django_crontab.management.commands.crontab

The crontab being generated looks like:

*/1 * * * * /Users/aendrew/Sites/django/ENV/bin/python /Users/aendrew/Sites/django/myproject/manage.py crontab run b036bdd89a12df2d0f85d87c9c01c8d7 #django-cronjobs for myproject

Any help? Thanks!

UNIX process doesn't die after cron job is finished.

While debugging server out of memory issues on my box, it seems like the UNIX process for crontab run for my job doesn't die after the job is finished. And for the next run, a new process is spawned eventually leading to a lot of live processes for the same cron job.

My CRON settings are like this -

CRONJOBS = [ ('*/1 * * * *', 'restserver.cron.my_task') ]
CRONTAB_LOCK_JOBS = True

The job is just a couple of log statements.

And every minute I see a new UNIX process being spawned with the same command -
/usr/bin/python /path/to/my//manage.py crontab run aaade9ebea079cbbe06c202ef28c67d

Is this expected ? How to make sure the process dies after the cron run instance is finished ?

Using for Database tasks

Hey @kraiz
first off thanks for making this amazing utility, its great & dead simple to use.

Actually I am django-crontab for a project, my use case is, I want to fetch data from an api, every 1 hour, and save it into a database using a Django model and migration stuff. So far I've been able to write a function which fetches data,

def getData():
    url = 'http://api.getdata.com/delhi' #sample url 
    r = requests.get(url)
    weather = r.json()
    data = {'weather'weather['data']}
    w_data = weathers()
    w_data.wdata = data
    w_data.save()

So the data variable is to be stored in database every 1 hour. So I'm kinda stuck how to do this every 1 hour,
should I add getData() in CRONJOBS in settings.py ?
This is where I'm unable to figure out how to use crontab in this use case, I'm quite new to python and django, it'll be a great help
Thanks ๐Ÿ˜„

Doesn't seem to work

Environment & Versions

  • Operating system: Linux (XUbuntu 16 TLS)
  • Python: 2.7
  • Django: 1.11.3
  • django-crontab: 0.71

Settings

  • My django-crontab settings:
CRONJOBS = [
    ('* * * * *', 'student_collaboration.cron.set_open_help_request_to_pending'),
    ('* * * * *', 'student_collaboration.cron.close_pending_help_requests_automatically_when_expired'),
]

Details

When I run python manage.py crontab add

  adding cronjob: (7931e4f0fc70fdb88ed51de09299094a) -> ('* * * * *', 'student_collaboration.cron.set_open_help_request_to_pending')
  adding cronjob: (da9d5e4cf8f5753ba16b9f786fd864b8) -> ('* * * * *', 'student_collaboration.cron.close_pending_help_requests_automatically_when_expired')
  • Output of crontab -l after running python manage.py crontab add.
* * * * * /home/jy95/Documents/oscareducation/ve/bin/python /home/jy95/Documents/oscareducation/manage.py crontab run 7931e4f0fc70fdb88ed51de09299094a # django-cronjobs for oscar
* * * * * /home/jy95/Documents/oscareducation/ve/bin/python /home/jy95/Documents/oscareducation/manage.py crontab run da9d5e4cf8f5753ba16b9f786fd864b8 # django-cronjobs for oscar

My projet tree:

/oscar (the main app)
       settings.py (where the crontab schedule are set)
/student_collaboration (sub app)
      cron.py (where the cron function are defined)

The github repo : https://github.com/JonathanSamelson/oscareducation/tree/prototype_2

importerror no module named myapp.cron

i set my env and script as README.rst
and my task in cron.py like:

...
...
def task1():
      log.info("i 'm task 1");#log msg to a file

when i run(i'm in vritual env)
python manage.py crontab add
and there is nothing occured
i checked linux env with command "crontab -e", it shows like

*/2 * * * *ใ€€/myvirtualenv/bin/python /myworkpath/manage.py crontab  run  c3a5f648ad78e794a12c664a

then i type /myvirtualenv/bin/python /myworkpath/manage.py crontab run c3a5f648ad78e794a12c664adirectly in terminal it's shows:

...
...
Importerror :no module named  myapp.cron

it's confused me ,what's root issue ?

Daemonizing a user's CRONJOBs

Few questions -

  1. Will the jobs run even after a user logs out ?
  2. Do I need to add the CRONJOBs eveytime the machine reboots ? Or once added it will continue using the schedule ?
    2a. How can I configure it with supervisor if need to add back everytime the machine reboots ?

Support django commands as cron jobs

It is convenient to have periodic jobs as django commands (some of them already are, e.g. "clearsessions").
But crontab currently allows to specify python functions only, which are effectively run inside a "crontab run" command.
Please remove this restriction.

Can't add crontab jobs in my staging server

Hi guys,
this may not be an issue, but I have a great doubt on how to make the management command work in my server

I receive
django@ip-xxxxxxxxxxx:/srv/venturefund.io/venturefund$ ./manage.py crontab add
no crontab for django
adding cronjob: (254d9abcc1aefdee36b6b5d329edbb56) -> ('0 2 * * 2 *', 'projects.cron.weekly_activity_digest.send_digest')
adding cronjob: (8fb9ca9f309c97b8a278ea1b49e04d2b) -> ('0 2 * * 2 *', 'django.core.management.call_command', ['set_new_investors_count'])
"/tmp/tmpFbC0M9":0: bad command
errors in crontab file, can't install.

I don't know what I did wrong. Can you help me understand what's going on?

Tracking jobs in Django site administration

Hi @kraiz

I scheduled a job using crontab and I would like to keep track of it in the Django site administration. I used to have a scheduled job using cron and I can see each performed task as I run

python manage.py migrate django_cron

If I try to do the same with django_crontab, I get: CommandError: App 'django_crontab' does not have migrations.
Is there a way to do something alike?
Thank you!

Crontab Logger issue

While the Crontab app executes as required, The logger of is not working properly. I have investigated and found that this is the comment that it shows "No handlers could be found for logger "django_crontab.crontab"" .

Can you please tell me what, if any, changes need to be done in the settings.py of my project to solve this issue.

Thanks

ImportError: No module named 'fcntl'

Hey,

I'm quite new with Django and I'm trying to use your work for my project. I followed your steps in the documentation but I am getting the error

ImportError: No module named 'fcntl'.

Am I missing any basic step in python installation? If it matters, I'm using Windows OS.
Hope you can help!

Thanks,
Matthew Go

Problem running crontab via run command

When I attempt to run job manually, I get an error:

MacBook-Pro-Aleksandr:beauty aperechnev$ python manage.py crontab run
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django_crontab/management/commands/crontab.py", line 29, in handle
    Crontab().run_job(options['jobhash'])
  File "/Library/Python/2.7/site-packages/django_crontab/crontab.py", line 126, in run_job
    job = self.__get_job_by_hash(job_hash)
  File "/Library/Python/2.7/site-packages/django_crontab/crontab.py", line 173, in __get_job_by_hash
    'Run "python manage.py crontab add" again to resolve this issue!' % job_hash
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!

But the job is registered and already in sync with django-crontab:

MacBook-Pro-Aleksandr:beauty aperechnev$ python manage.py crontab show
Currently active jobs in crontab:
2755b3a03042fdf7b9c8edbcaff737f6 -> ('0 * * * *', 'grabber.cron.retrieve_new_images')

Dont remove cronjobs when adding

Hi there, Im assuming the remove_cronjobs is executed when running "crontab add" to make sure that no duplicates are inserted, but it would be nice to find a better way somehow.

The reason is that I have multiple django instances on the same server, and apparently the command_lines array that django_crontab uses applies to all of them, not specifically to a particular django instance.. so, whenever I try to add cronjobs from one instance, all the jobs from the other one are removed first, which is not so nice.

Cheers

Jobs not being executed

I'm sorry for making this a issue but I couldn't find any answers anywhere and no contact info for any of the contributors.

I followed the readme and was able to register my cronjobs and add my crontabs without it reporting any errors, but, no jobs are being executed. I even tried creating a log file but that's not being created also.

System:
Python 2.7.12
Django 1.8.14
django-crontab 0.7.1

django-crontab logging doesn't appear to use the default timezone

Hi @kraiz, thanks for your work on this project. I've been using django-crontab for the last year, it's simple and pretty much Just Works. However I've noticed recently that django-crontab logging doesn't appear to use the default timezone.

settings.py

TIME_ZONE = 'America/Toronto' # -05:00 EST
USE_TZ = True
CRONJOBS = [
    ('0 9 * * *', 'myapp.cron.daily_order_summary', '>> cron.log'),
]

cron.py

from django.utils import timezone

def daily_order_summary():
    now = timezone.now()
    print('running daily_order_summary ({})'.format(now))

cron.log

running daily_order_summary (2017-01-11 14:00:03.304917+00:00)

I was expecting cron.log to be

running daily_order_summary (2017-01-11 09:00:03.304917-05:00)

Here's my server time a few minutes after running the above cron, note the "EST"

django@host:~$ date
Wed Jan 11 09:03:39 EST 2017

I'm running

  • ubuntu 14.04
  • django 1.8.x
  • django-crontab (0.7.1)

To be clear, the cron job runs at the correct time, but the logging output for some reason isn't displaying the correct timezone.

"Format #1" treats output redirection option as input to module function

Hi, first thanks for working on this django extension.

I tried using format #1 like this:
CRONJOBS = [
('*/1 * * * *', 'myapp.cron.cron_job', '> /home/myhome/logs/cron_job.log')
]

My method cron_job() takes no argument
django_crontab's logging error saying cron_job takes no argument but 32 given in this case. somehow your code is trying to pass the redirection path into my method as argument and treating each character as an individual argument?

Hash function to reference jobs seems not stable

Writing and running tests showed that equal job tuples from CRONJOBS does not always generate the same hash. For the following job i found different hashs generated:

('*/1 * * * *', 'tests.cron.cron_job', [1, 'two'], {'a': 's2', 'test': 34})
fe2f7249477f5c7270f08a7a74733c98
13e8169dffe273b8b0c5f8abe1b6f643

This needs some further investigation as tests sometimes pass, sometimes not depending on which hash gets generated.

cron not working even though it has a task to run

Everything went perfectly when implementing django-crontab to my project. After executing the python manage.py crontab add, it adds the job to crontab (I checked using crontab -l) but crontab doesn't seem to be doing anything. There are no errors and a log isn't even generated. I'm using the django 1.6 with python 2.7.

Commands in the project directory:

$ python manage.py crontab add
adding cronjob: (1ac092f177e6b21c85c301d59022d7f6) -> ('* * * * *', 'roomer.cron.my_scheduled_job', [], {}, '>> /home/isimar/project/roomer/cron_log/my_scheduled_job.log 2>&1')

$ crontab -l

  • * * * * /home/isimar/.virtualenvs/django16/bin/python /home/isimar/project/manage.py crontab run 1ac092f177e6b21c85c301d59022d7f6 >> /home/isimar/project/roomer/cron_log/my_scheduled_job.log 2>&1 # django-cronjobs for NextDroidLabs

$ python manage.py crontab run 1ac092f177e6b21c85c301d59022d7f6
(Works - does what its suppose to do, so the problem seems to be with the crontab)

Help Please

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

Error when trying with django 1.10 and python2

I have the issue when im in console

ubuntu@ip-XXX-XX-XX-XXX:~/connectus2$ sudo python manage.py crontab run
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 342, in execute
    self.check()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/ubuntu/connectus2/connectus2/urls.py", line 16, in <module>
    from django.conf.urls import patterns, url
ImportError: cannot import name patterns

I solved downgrading to 1.9

ImportError: No module named manage

Although I have defined the CRONTAB_DJANGO_MANAGE_PATH variable on the settings.py

Any idea?

Traceback (most recent call last):
File "private/devcon/manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 443, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
import(name)
File "/usr/local/lib/python2.7/dist-packages/django_crontab-0.3-py2.7.egg/django_crontab/management/commands/crontab.py", line 3, in
from django_crontab.app_settings import CRONTAB_EXECUTABLE, CRONJOBS,
File "/usr/local/lib/python2.7/dist-packages/django_crontab-0.3-py2.7.egg/django_crontab/app_settings.py", line 20, in
DJANGO_MANAGE_PATH = getattr(settings, 'CRONTAB_DJANGO_MANAGE_PATH', import_module(DJANGO_PROJECT_NAME + '.manage').file.replace('pyc', 'py'))
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
import(name)
ImportError: No module named manage

FileNotFoundError

Running cron with activated virtualenv using /usr/local/www/project/venv/bin/python /usr/local/www/project/app/manage.py crontab run a2cb3e38b81be23bf313e171a630b684
works without any error.

But in the crontab-Log there are is a FileNotFoundError

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpflqhnzil/texput.pdf'

Maybe a bug? Would be great if the cronjob in crontab behaves like when being manually executed in the virtualenv

Python 3.4
Django 1.9.8
django-crontab 0.7.1

bad command

i can run the command 'add' in my own laptop(Mac), while i can't run it on the centos(7.0) server.
it's always show notification 'bad command'.
ps: we are using django 1.9
django-crontab 0.7.0

Example of a management command

@jiangmf @kraiz am running a management command manually like this python manage.py check_celery_status
but unable to run it through this module. then command lies in a file under myapp.management.commands.command_file

class Command(BaseCommand):
    def handle(self, *args, **options):

        """
        The command to check if celery is up
        """
        date = dt.now().date()
        print str(date)

Cron Jobs was executed but doesn't affect the database

Environment & Versions

  • Operating system: Ubuntu 16.04
  • Python: python 3.5
  • Django: 1.11.9
  • django-crontab: 0.7.1
  • Database: sqlite

Settings

  • My django-crontab settings:
CRONJOBS = [
    ('*/1 * * * *', 'bitcoin_rate_monitor.main.cron.update_rates')
]
  • My Cron Script:
def update_rates():
    from ..main.models import Provider
    provider_set = Provider.objects.all()
    for provider in provider_set:
        provider.do_update_rate()

Details

  • Output of crontab -l after running python manage.py crontab add.
*/1 * * * * /usr/bin/python3 /vagrant/bitcoin_rate_monitor/manage.py crontab run efc4cde65aae2b54915c6b3d90d3a4bc # django-cronjobs for config
  • Manually running the manangement command works and updates the database, but when the cron job executed, nothing happened.
/usr/bin/python3 /vagrant/bitcoin_rate_monitor/manage.py crontab run efc4cde65aae2b54915c6b3d90d3a4bc
  • Running 'grep cron /var/log/syslog' shows:
Jan  7 06:39:01 ubuntu-xenial CRON[1822]: (ubuntu) CMD (/usr/bin/python3 /vagrant/bitcoin_rate_monitor/manage.py crontab run efc4cde65aae2b54915c6b3d90d3a4bc # django-cronjobs for config)
Jan  7 06:40:01 ubuntu-xenial CRON[1837]: (ubuntu) CMD (/usr/bin/python3 /vagrant/bitcoin_rate_monitor/manage.py crontab run efc4cde65aae2b54915c6b3d90d3a4bc # django-cronjobs for config)

Django Crontab with deployment environment

I'm developing a module with the crontab.

Actually, the framework I'm using is django so I did install 'django-crontab'

I did test as the instruction did and make it with localhost environment.

When I deployed("sudo service apache2 restart") it on AWS after doing a command 'python manage.py crontab add', it didn't work.

I thiknk it's working on only localhost environment, isn't it?

How can I solve this problem?

run in another settings

Would I run different context?

diff --git a/django_crontab/app_settings.py b/django_crontab/app_settings.py
index 2623aca..4450699 100644
--- a/django_crontab/app_settings.py
+++ b/django_crontab/app_settings.py
@@ -41,3 +41,5 @@ COMMAND_PREFIX = getattr(settings, 'CRONTAB_COMMAND_PREFIX', '')
 COMMAND_SUFFIX = getattr(settings, 'CRONTAB_COMMAND_SUFFIX', '')

 LOCK_JOBS = getattr(settings, 'CRONTAB_LOCK_JOBS', False)
+
+CRONTAB_SETTINGS_MODULE = getattr(settings, 'CRONTAB_SETTINGS_MODULE', os.environ['DJANGO_SETTINGS_MODULE'])

diff --git a/django_crontab/management/commands/crontab.py b/django_crontab/management/commands/crontab.py
index f0a6ed6..e0ece51 100644
--- a/django_crontab/management/commands/crontab.py
+++ b/django_crontab/management/commands/crontab.py
@@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand
 from django.utils.importlib import import_module
 from django_crontab.app_settings import CRONTAB_EXECUTABLE, CRONJOBS, \
     CRONTAB_LINE_PATTERN, CRONTAB_COMMENT, PYTHON_EXECUTABLE, DJANGO_MANAGE_PATH, \
-    CRONTAB_LINE_REGEXP, COMMAND_PREFIX, COMMAND_SUFFIX, LOCK_JOBS
+    CRONTAB_LINE_REGEXP, COMMAND_PREFIX, COMMAND_SUFFIX, LOCK_JOBS, CRON_SETTINGS_MODULE
 import fcntl
 import hashlib
 import json
@@ -81,14 +81,15 @@ class Command(BaseCommand):
                 'comment': CRONTAB_COMMENT,
                 'command': (
                     '%(global_prefix)s %(exec)s %(manage)s crontab run '
-                    '%(jobname)s %(job_suffix)s %(global_suffix)s'
+                    '%(jobname)s %(job_suffix)s %(global_suffix)s --settings=%(settings_module)s'
                 ) % {
                     'global_prefix': COMMAND_PREFIX,
                     'exec': PYTHON_EXECUTABLE,
                     'manage': DJANGO_MANAGE_PATH,
                     'jobname': self.__hash_job(job),
                     'job_suffix': job_suffix,
-                    'global_suffix': COMMAND_SUFFIX
+                    'global_suffix': COMMAND_SUFFIX,
+                    'settings_module': CRONTAB_SETTINGS_MODULE
                 }
             })
             if options.get('verbosity') >= '1':

Warning about import_module deprecation

/myapp/lib/python2.7/site-packages/django_crontab/crontab.py:13: 
RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  from django.utils.importlib import import_module

Just to warn

Cron fails to log, while calling the function in django's shell works

I have a tasks.py file that's located in project/app/tasks.py that uses the standard logger:

import logging
logger = logging.getLogger(__name__)

The logging file is configured with loggers for all of the following:
django, app, django_crontab, django_crontab.crontab, crontab, and ''

When is open the shell and run the function, the logging appears:

python manage.py shell
import django
django.setup()
from app.tasks import my_scheduled_task()
my_scheduled_task()

Yet, when I add the cron job using python manage.py crontab add everything works (I can see the end result appear), yet nothing gets logged. Any reason why logging should work using the shell, but not as a cron?

run method with arguments

myapp/cron.py

def my_scheduled_job(value):
        print('hello %s' % value)

I tried the flow code:

CRONJOBS = [
    ('*/5 * * * *', 'myapp.cron.my_scheduled_job("world")' ),
    ('*/5 * * * *', 'myapp.cron.my_scheduled_job("python")' ),
]

return AttributeError: module 'myapp.cron' has no attribute 'my_scheduled_job("world")'

Can't get the cron to run properly

Environment & Versions

  • Operating system: macOS Sierra 10.12.5
  • Python: 3.6.1
  • Django: 1.11.1
  • django-crontab: 0.7.1

Settings

  • My django-crontab settings:
CRONJOBS = [
  ('*/1 * * * *', 'indicei3.cron.test_cron', '>> /tmp/scheduled_job.log')
]

Details

  • Output of crontab -l after running python manage.py crontab add.
  adding cronjob: (14916de3431e91ed32d3691cfe1be719) -> ('*/1 * * * *', 'indicei3.cron.test_cron', '>> /tmp/scheduled_job.log')

  • If this output contained a crontab run command and I copy this command to my terminal the job works: yes/no

No

I have been searching for a solution for some hours now but I couldn't find anything

This is my cron.py:

def test_cron():
    print("Hello")

I start the server but nothing happens. The file under tmp/schedule_job.log does not get written.

Thanks in advance.

Log Is Always Empty

settings.py:

# django-crontab
CRONJOBS = [
    ('* * * * *', 'owl.cron.my_scheduled_job', '>> cron.log')
]

cron.py:

from django.utils import timezone

def my_scheduled_job():

    now = timezone.now()
    print('running daily_order_summary ({})'.format(now))

I can tell that the job is running, because "cron.log" is created every minutes, but it's always empty.

Please advise?

Running with gunicorn

Hey @kraiz ๐Ÿ˜ƒ

I've been using django-crontab since three months now and its super awesome. I just wanted to ask if there is any special setting required to run cron jobs inside gunicorn.

Actually my scenario is that we want to make some HTTP requests through the cronjob. Now while running Django Development Server, cron jobs are working perfectly. But the issue is when I run them in Docker + gunicorn setup, the cronjobs get registered (checked using crontab -l) alright, but none of them work.

Here's what I've been trying,

  • Removing HTTP requests code and adding simple DB manipulation code inside my cron.py, it doesn't work either.
  • Using gevent in gunicorn for asynchronous workers.

Do yo think its something related to worker processes in gunicorn, I'm having absolutely no idea about this even after a week of googleing. Any help or pointers would really be great!

Thanks a lot!

Add support for different projects cronjobs

I've many projects on the same machine, each one has its own virtualenv.

If I add a cronjob for just 1 project everything works fine, but when I add a cronjob of another project it replaces the previously added cronjob.

I think the reason is that CRONTAB_DJANGO_MANAGE_PATH and CRONTAB_PYTHON_EXECUTABLE are different for each project, then there should be the possibility to specify these paths for each cronjob to avoid paths overwrite errors.

log stderr?

Hi, I'm wondering if there is a simple way to log stderr?

Recently discovered a cronjob that was silently failing for a few weeks and caused some headaches and would like to avoid this in the future.

pip install fails

Old version README is making problems in pip, i cannot install it

Downloading/unpacking django-crontab
  Running setup.py egg_info for package django-crontab
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "$SOME_PATH$/build/django-crontab/setup.py", line 12, in <module>
        long_description=open('README.md').read(),
    IOError: [Errno 2] No such file or directory: 'README.md'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "$SOME_PATH$/build/django-crontab/setup.py", line 12, in <module>

    long_description=open('README.md').read(),

IOError: [Errno 2] No such file or directory: 'README.md'

----------------------------------------
Command python setup.py egg_info failed with error code 1 in $SOME_PATH$/toopo/build/django-crontab

Django 1.9 support

Yesterday Django 1.9 came out and I am still getting warnings on django_crontab. Will you support django 1.9. I believe there is just this minor fix needed.
python/virtenv/lib/python2.7/site-packages/django_crontab-0.6.0-py2.7.egg/django_crontab/crontab.py:13: RemovedInDjango19Warning: django.uti$ from django.utils.importlib import import_module
Will new version of django-crontab be soon available with pip?
here is old issue - #25

Where is my crontab executable

Hello there

Running ubuntu16, django1.10 through docker.

Can anyone tell me what the path to the crontab executable is, or how I find it?

I'm getting this error running crontab add

sh: 1: /usr/bin/crontab: not found
adding cronjob: (d5fc228456872d633a9fd2f18a2ace15) -> ('*/5 * * * *', 'research.cron.update_feeds')
sh: 1: /usr/bin/crontab: not found

which crontab on the server replies with nothing

On a non docker ubuntu16 which crontab replies with /usr/bin/crontab

I think it's because I must be root on the container.

Anyone got any ideas on how to get around this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.