django-cron task not executing automatically - python

When I run python manage.py runcrons, the cron job successfully executes (prints 'Executed'). But it doesn't do it automatically. It's supposed to execute every minute as I've stated in the Class. Here's the code:
settings.py
CRON_CLASSES = [
"app.views.MyCronJob"
]
app.views
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 1
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'my_app.my_cron_job' # not sure what this is supposed to be?
def do(self):
print('Executed')
Any idea?

you also need to set up the crontab entry for the cron:
> crontab -e
*/5 * * * * source /home/ubuntu/.bashrc && source /home/ubuntu/work/your-project/bin/activate && python /home/ubuntu/work/your-project/src/manage.py runcrons > /home/ubuntu/cronjob.log
taken from official documentation

+1 for Ayush's answer. This worked for me.
crontab -e
This will open the editor, copy paste the following line at the bottom and save & close.
*/5 * * * * source /home/ubuntu/.bashrc && source /home/ubuntu/work/your-project/bin/activate && python /home/ubuntu/work/your-project/src/manage.py runcrons > /home/ubuntu/cronjob.log
In addition to this try rerunning these in your Django project directory:
python manage.py runcrons --force
service cron start

Related

Crontab not finding python modules

Hello i have been following stackoverflow posts and im unable to get the crontab to run my python scripts in ubuntu.
After talking with the user below, i have modified my file but am not getting a "moduleNotFoundError: No module named aioredis"
when i deactivate my py3.9 venv environment, and call python feed_ingestor_to_postgres.py i get the same error. so clearly the pipenv isn't activating for me. any suggestions?
my launcher.sh file:
#!/bin/bash
WORKDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $WORKDIR
activate(){
../../env/bin/activate
}
python3 feed_ingestor_to_postgres.py
my crontab file:
* * * * * echo "Great cron, awesome job!" > /var/log/nonsense
* * * * * /home/ubuntu/phobos/phobos-trading-engine/exchange_feeds/launcher.sh
I have no idea what else to do here. any help would be appreciated
To manage python scripts from the crontab more easily, I would advise to use a simple shell wrapper script, for example "launcher.sh" which will be called by the crontab. The launcher will cd to the script location, manage the virtualenv, or possibly set env vars if required:
#!/bin/bash
WORKDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $WORKDIR
source ../../env/bin/activate
python feed_ingestor_to_postgres.py
Then in the crontab, call the launcher instead of your python script:
* * * * * ~/path/to/launcher.sh
It will be much more flexible than editing the crontab directly

Crontab not running script automatically

I created a custom python command called expire_lesson.py. In my terminal when I run python3 manage.py expire_lesson, the command successfully executes. I have added a cron job * * * * * cd /Users/james/Desktop/elearning && python3 manage.py expire_lesson to run the script every minute. The issue is the cron job is not working. I believe it has something to do with the location of my cron job, but am unsure how to find the exact location, or if there is another issue. I would greatly appreciate any help in adding a cron job that runs expire_lesson successfully.
class Command(BaseCommand):
help = 'Expires old lesson objects'
def handle(self, *args, **options):
Lesson.objects.filter(lesson_end__lt=timezone.now()).delete()
try with full path off python3 and then update your crontab line with full python3 path something like...
* * * * * cd /Users/james/Desktop/elearning && /usr/bin/python3 manage.py expire_lesson

Beanstalk Cronjobs using django-cron

Im trying to get cronjobs running on my elasticbeanstalk ec2 instance using django-cron.
I created a cronjob.cron:
*/5 * * * * root /opt/python/run/venv/bin/python3 /opt/python/current/app/manage.py runcrons >> /var/log/cronjobs.log
and added the file using commands:
container_commands:
06_runcrons:
command: "cat .ebextensions/cronjobs.cron > /etc/cron.d/djangocrons && chmod 644 /etc/cron.d/djangocrons"
leader_only: true
The cron seems to run but nothing gets printed into my /var/log/cronjobs.log (Which it should, every 5mins).
I cannot test the script on the ec2 instance manually, because my environment variables I added using aws:elasticbeanstalk:application:environment: are neither present with the ec2-user or root. Where do those environment variables get written to? Are they only present during the deploy?
The server itself works fine, so my db environment variables work.
Do I need to use another user for my cronjobs? How can I solve this?
Edit:
Removing the user like:
*/5 * * * * /opt/python/run/venv/bin/python3 /opt/python/current/app/manage.py runcrons >> /var/log/cronjobs.log
results in
(CRON) bad username (/etc/cron.d/djangocrons)
in my /var/log/cron
Solved it by adding the file that sources the env variables in /opt/python/current/env
*/5 * * * * root source /opt/python/current/env && /opt/python/run/venv/bin/python3 /opt/python/current/app/manage.py runcrons >> /var/log/cronjobs.log

schedule django custom commands with cron inside a virtualenvironment

I have written a custom management command post_message when I run python manage.py post_message the command executes well.
Now I want this command to run every 10 minutes.
I have a virtual envrironment.
I have a file - msg.cron the contents of the file are as follows -
#!SHELL=/bin/bash
*/10 * * * * source /home/username/Envs/project_name/bin/activate && /home/username/Code/project_name/manage.py post_message > /dev/null
I have done chmod +x on msg.cron
Having done this, I added
crontab msg.cron
Now when I do crontab -l contents of msg.cron are shown.
But the management command are not being run, what am I missing?
You don't need to activate the virtualenv in this case. You can just use the python in the bin directory of the virtualenv.
*/10 * * * * source /home/username/Envs/project_name/bin/python /home/username/Code/project_name/manage.py post_message > /dev/null

How to run custom manage.py in crontab + virtual env?

How to run in crontab
*/1 * * * * /home/user/Desktop/job/dp/ python manage.py statistics
with virtual env? I need to activate virtualenv first(Otherwise it does not work)
This is my virtual env:
source job/bin/activate
EDITED:
Try something like this:
*/1 * * * * . /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics
This should be read as: activate the env and if that was successful, excute the manage.py script. Since manage.py is supposed to have a python shebang and the virtual env sets the correct python interpreter, this should work.
Apparently cron usually runs with /bin/sh which does not know the source command. So one option is to use a dot as a source replacement. Another to set /bin/bash in the cron file:
SHELL=/bin/bash
*/1 * * * * source /path-to-env/bin/activate && /home/user/Desktop/job/dp/manage.py statistics
Read more about this issue at:
http://codeinthehole.com/writing/running-django-cronjobs-within-a-virtualenv/
The article doesn't mention that source can be replaced by a ., but i've just tried it and it worked for me. So you have several options to choose from now, the article even has others. ;)
Use something like ~/envs/someenv/lib/python /path/to/your/script
In your situation it will look like
*/1 * * * * ~/envs/someenv/lib/python /home/user/Desktop/job/dp/manage.py statistics

Categories