Crontab not running script automatically - python

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

Related

Crontab is running but still not executing command Django

I am trying to execute a command through Django Crontab everyday. Here is what I am doing:
First, I added django_crontab in INSTALLED_APPS
FYI, I have written a Django command sendalerts which is working perfectly fine
Now I am trying to run that command through crontab on regular intervals
This is what I added in my settings.py
CRONJOBS = [
('* * * * *', 'django.core.management.call_command', ['sendalerts']),
]
When I run this command through python manage.py crontab add it doesn't give any error. It also list down cronJob when I check with this command python manage.py crontab show
But problem is it doesn't execute the code which is written in my sendalerts command.
What can I do to check what is that I am doing wrong or what can be the error which I can fix to make it work?
Edit:
Output of crontab -e is
* * * * * /usr/local/bin/python /home/wukla/app/app/manage.py crontab run 455e70156896954803547b6f6d845f9b # django-cronjobs for app
There can be multiple issue with this. The one's I have encountered are:
1) If your script is using any PATH variable from system then that has to be added to crontab manually.
2) You should add a relative path to your script to run.
3) Crontab entry should always have a newline at the end of the file.
These are all I thing I have come across as errors while using crontab.
Hope this might help you.

What's the proper way to set up my path in crontab?

I'm new to crontab and having some trouble.
I know that the set up is
* * * * * command to execute
and that the * stand for min, hour, day of month, month, day of week respectively
Usually, to run my code in the terminal I do
source /Users/mmmm/PycharmProjects/kt/venv/bin/activate
cd tests
python test1.py
but I'm not sure how to set that up on crontab. What's the proper way to do so?
You can create a bash script. Let's call it script.sh
Inside script.sh you'll have
source /Users/mmmm/PycharmProjects/kt/venv/bin/activate
cd tests
python test1.py
Make sure you make the script executable with chmod +x script.sh!
Then you can add that script to crontab with
* * * * * /path/to/script.sh
This will activate the the venv, cd into tests and run test1.py while only putting the bash script into cron.
Here's a line I used for a django application management command
0 */2 * * * source /Users/jeff/.virtualenvs/myvirtualenv/bin/activate && /Users/jeff/ecommapp/manage.py amzn_scrape H >> /Users/jeff/ecommapp/log/scraper_results.log 2>&1
Activate the env and then call the command - last logging part is optional.

django-cron task not executing automatically

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

Crontab does not run my python script usi.py

I checked a lot posts with the same title, but I can't get my python running via cron.
I have several cron scripts already, which execute well, but not python.
Crontab runs as root.
I added following lines to crontab:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
I have this line for the execution in crontab:
* * * * * cd /var/www/usi/; /usr/local/bin/python3.6 /var/www/usi/usi.py
I tried a lot of variations:
added sudo in front of it to run as a different user
added bash to the line
executed the user profile before etc etc.
No results.
No errors in system log.
Any ideas?
Using Debian8
I found the error while putting the cron task into a separate shell script. I executed the script via cron and got an error. Simple typo. Did now saw any error before in syslog. :-(
It executes well with this in crontab now:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * cd /var/www/usi/ && /usr/local/bin/python3.6 /var/www/usi/usi.py

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

Categories