Can't run crontask in django project - python

I have django problem and I have problem with run the .sh file on server but not on local machine.
My .sh file:
#!/bin/bash
source $HOME/.profile
cd /home/myproject/test
source venv/bin/activate
cd www
python manage.py make_import
I want to run this .sh file and run import with this line in crontab (Ubuntu):
10 8 * * * /home/myproject/test/www/crontab/test/make_import.sh
But this isn't working.
When I do this on my local computer via python manage.py make_import everything is fine.
Thanks for any hint.

Related

How to create an executable file with multiple commands?

I want to create an executable file with various commands that run a project in django along with a virtual environment. The commands I want to run are:
d:
cd Python/projects
myvenv\Scripts\activate
cd web
python manage.py runserver
You can chain those commands like this in Windows machine:
Python\projects\myenv\Scripts\activate && python web\manage.py runserver

Run a crontab for django management command with an argument

I wish to ping_google for the sitemap every 10 minutes using crontab and django manage.py
I need to run the following on server-
python manage.py ping_google /sitemap.xml
I am not sure how to give the parameter '/sitemap.xml' in the crontab. Here is what I have
*/10 * * * * /home/project/.virtualenvs/app/bin/python /home/project/workspace/app-backend/django-app/manage.py ping_google /sitemap.xml
Am I passing the argument correctly? How am I supposed to pass the argument?
I think it might be better here to write a small shell script here that sets the current working directory correctly, and activates the virtual environment. For example a file command.sh in the directory /home/project/workspace/app-backend/django-app/:
#!/bin/bash
# cd to the correct directory
cd "/home/project/workspace/app-backend/django-app/"
# source the virtual environment
. /home/project/.virtualenvs/app/bin/activate
# call the command
python manage.py ping_google /sitemap.xml
Then you can run this with:
*/10 * * * * bash /home/project/workspace/app-backend/django-app/command.sh

Run python script on cron using conda

Trying to send some output to Slack using cron on an instance of GCP Compute Engine running Linux Ubuntu 18.04.2 LTS.
Output is generated by python script.
Python script is usually run using conda activate my_env and python my_script.py
I have made the bash file executable by doing chmod +x my_script.bash
Here is content of bash file:
#!/bin/bash
source /home/user/miniconda3/bin/activate
conda activate my_env
python /home/user/folder/cron/reports.py -r check_stocks
I would expect adding the following line to crontab -e:
00 21 * * * cd /home/user/folder/cron/ && /bin/bash my_script.bash would give me the same results.
I run cd /home/user/folder/cron/ && /bin/bash my_script.bash in my shell and the script runs fine.
Make your .py file exacutable too (chmod +x file.py) - otherwise it won't work.
You can find similar issue resolved here.
I had a similar issue and I gave up activating the conda environment and instead called directly the python bin in the miniconda environment folder, like this:
00 21 * * * /home/myuser/miniconda3/envs/my_env/bin/python /home/user/folder/cron/reports.py
I don't think this is the recommended solution, especially if you have a complex project with dependencies and you are importing a lot of modules, but for my simple script it solved the problem.

How to properly run virtualenv via .sh run script (django)

I am having an issue via an apache Nearly Free Speech server (I have been following the NFS guide via this link: https://blog.nearlyfreespeech.net/2014/11/17/how-to-django-on-nearlyfreespeech-net/. I have created a run-django.sh file to properly run the application which also opens a virtualenv (I have named the folder 'venv'). These are the contents of my run-django.sh file:
#!/bin/sh
. venv/bin/activate
exec python3 manage.py runserver
On the current step of the guide I am attempting to run the run-django.sh as follows:
[questionanswer /home/protected]$ ls
question_answer run-django.sh venv
[questionanswer /home/protected]$ cd question_answer/
[questionanswer /home/protected/question_answer]$ ../run-django.sh
.: cannot open bin/activate: No such file or directory
How is this not detecting my directory of 'venv/bin/activate' ?

How do I run a Python script that is part of an application I uploaded in an AWS SSH session?

I'm trying to run a Python script I've uploaded as part of my AWS Elastic Beanstalk application from my development machine, but can't figure out how to. I believe I've located the script correctly, but when I attempt to run it under SSH, I get an import error.
For example, I have a Flask-Migrate migration script as part of my application (pretty much the same as the example in the documentation), but after successfully SSHing to my EB instance with
> eb ssh
and locating the script with
$ sudo find / -name migrate.py
when I run in the directory (/opt/python/current) where I located it with
$ python migrate.py db upgrade
at the SSH prompt I get
Traceback (most recent call last):
File "db_migrate.py", line 15, in <module>
from flask.ext.script import Manager
ImportError: No module named flask.ext.script
even though my requirements.txt (present along with the rest of my files in the same directory) has flask-script==2.0.5.
On Heroku I can accomplish all of this in two steps with
> heroku run bash
$ python migrate.py db upgrade
Is there equivalent functionality on AWS? How do I run a Python script that is part of an application I uploaded in an AWS SSH session? Perhaps I'm missing a step to set up the environment in which the code runs?
To migrate your database the best is to use container_commands, they are commands that will run every time you deploy your application. There is a good example in the EBS documentation (Step 6) :
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
The reason why you're getting an ImportError is because EBS installs your packages in a virtualenv. Before running arbitrary scripts in your application in SSH, first change to the directory containing your (latest) code with
cd /opt/python/current
and then activate the virtualenv
source /opt/python/run/venv/bin/activate
and set the environment variables (that your script probably expects)
source /opt/python/current/env

Categories

Resources