Crontab job not executing in django project - python

I am using crontab to execute certain function, however when i run the server the job is not processed. When i use the command crontab show , it shows me the job , bit again when i run the server its not executed. I followed this documentation https://pypi.org/project/django-crontab/ and watched YT tutorial as well but nothing worked.
crontab -l. shows this : '*/1 * * * * /Users/nourelgeziry/.local/share/virtualenvs/Equal_Distribution_maintainance-M-bcKwQz/bin/python /Users/nourelgeziry/Desktop/MyProjects/Equal Distribution maintainance/EDMS/manage.py crontab run bd84e5bec9ad4805b334e11fafec1b5c # django-cronjobs for EDMS' .
Also worth mentioning i am using pipenv for the virtual env.
Let me know if there is anything i can provide more . Thank you in advance

I tried to use Django crontabs as well before, but I never managed to get them working.
What I did instead was creating custom Django commands that could be executed using for example 'python3 manage.py send_emails' (if send_emails was the name of my custom command).
How to create custom Django commands is documented here: https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/
So, here's what I did:
1 - Create a custom django command
2 - Create a sh file that cd into your Django project directory and runs python3 manage.py name_of_your_command
3 - Set up a cronjob to execute the sh file periodically

Related

Can a Django app be run directly from python?

I have created an app with Django, on my Raspberry Pi, as part of my smart home project. Now that I am coming to the end of the project, I would like to make it so that I can run one script and have all of the features of the smart home working. (This would be especially useful as then I can just make it run on boot without having to connect a screen or ssh).
What I would like to know is if there is a way I can make a python script that will run the whole Django app so that I don't have to go into terminal and set it up. Is this possible? and if not what is the best way to go about my issue?
Yes you can do that you set the autologin in raspi-config then create a shell script run.sh
like this
python /path/to/project.manage.py runserver 0.0.0.0:8000
or you can use gunicorn proxy
You can also start other servers in this file
then put sh run.sh in you .bashrc.
Better way
You can install tmux by apt-get install tmux
You can make detached session using tmux in run.sh using a unique name and tmux so that each time you open the prompt it wont create duplicate sessions.

Run Python script on startup/reboot of AWS instance

I'm running Flask on an AWS instance. My goal is to be able to have Flask running on its own, without me having to ssh into it and run
python app.py
Is there a way to have this command run every time the AWS instance itself reboots?
Yes there is a way to start the python script on reboot.
On linux you will find /etc/init.d directory. You will need to write your own init.d script and put it inside /etc/init.d directory,which will indeed start your python script. Ahh ! wait its not goning to be magic. Dont worry, there is fixed format of init.d script. Script contains some basic tasks like start(),stop(),reload() etc. Just add the code that you want to run on start in start() block.
Some reference link : https://bash.cyberciti.biz/guide//etc/init.d
Try this:
(crontab -l 2>/dev/null; echo '#reboot python /path/to/app.py') | crontab -

Run python script right after RPi boot

I'm relatively new to raspberry pi (5 days using it) and I've just finished to run my python script succesfully (called dogcare.py). Now I'm trying to execute this script right after my raspberry is turned on. I've been doing some research and I find different ways to do it:
using /etc/profile
using /etc/rc.local
using crontab
using /etc/init.d
using systemd
But none of these ways are working for me.
Setup enviroment:
Hardware: RaspberryPi 2 Model B
Software: Raspbian or NOOBs (not sure)
Context:
Since for my project I need to run meet.jit.si, I followed this guide http://www.instructables.com/id/Video-Calling-on-Raspberry-Pi-3/?ALLSTEPS and It has a step where sets chromium website to start right after RPi is turned on. (Currently this is working fine)
My python script is using request library in order to use HTTP GET with an external website application I've been working on.
Main problem:
I need to run both events: chromium website with meet.jit.si and my python script when my raspberry is turned on.
Current situation: chromium website is running after my RPi is turned on but my script doesn't.
I'd appreciate any help !
I have done a similar thing with my Raspi 2 as well which involved sending myself an email with the ip address of the pi so I could easily ssh/vnc to it.
My steps involved making a shell script which ran the python program.
#!/bin/sh
cd pythonfiledirectory
sudo python pythonfile.py
cd /
Then I made it executable with the following command:
chmod 777 file.sh
Now edit your crontab to run the file on startup.
In your terminal, type:
sudo crontab -e
Inside of the crontab write:
#reboot sh file.sh
You could add a log file if you wanted to debug and see why it's not working by making a log directory and changing the text you wrote in the crontab to:
#reboot sh file.sh >/logdirectoy/ 2>&1
This is what made it work for me and if it doesn't work try and make sure you can run your .sh file and try the crontab with some other files to debug the problem.

Saving changes in docker project

I just started a django project with docker, I used the cookiecutter-django template that is discussed in the book Two scoops of django.
I am trying to set it all up in OSX, but I am having some trouble with the following part from the documentation:
Saving changes
If you are using OS X or Windows, you need to create a
/data partition inside the virtual machine that runs the docker deamon
in order make all changes persistent. If you don’t do that your /data
directory will get wiped out on every reboot. To create a persistent
folder, log into the virtual machine by running:
$ docker-machine ssh dev1
$ sudo su
$ echo 'ln -sfn /mnt/sda1/data /data' >>
/var/lib/boot2docker/bootlocal.sh
However, if I execute these commands, and try to start my docker project, I get the following error:
ERROR: Cannot start container 182a38022fbdf65f7a64f1ca5475a3414084d11c91f1cb48bffc6f76491baf4a: mkdir /data: file exists
I'm quite stuck at this point, do you guys have an idea what I could do to get this up and running?
So in the end this was fixed by making the directory in the local machine. I did that with the following code when adding the line to the bootlocal.sh file:
$ mkdir /mnt/sda1/data

Issue in running user-data script AWS EC2

Is it possible to run python script through AWSCLI (user-data). I tried but it didn't run and i have following in my logs
boot.log:2015-08-07 10:08:30,660 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: './step-1
cloud-init.log:2015-08-07 10:08:30,660 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: './step-1'
cloud-init-output.log:2015-08-07 10:08:30,660 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: './step-1'
Note step-1 is my script which i am trying to pass as user-data . Also my script is present in same directory from where i am running command so it should pick
The default interpreter seems to be Python. So if you simply want to execute a shell script you'll need to start with a hash-bang, for example:
#!/bin/bash
/yourpath/step-1
Please note, in order to debug this, try: cat /var/log/cloud-init-output.log
And see: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts
You can use any command to be run under user-data. I have used user-data to bootstrap Windows Instances with Domain Controller setup or domain join using PowerShell; of course given that it is on EC2 - the properties are extensible whether you are running in Unix based or Windows Based.
So you have specified, Python - so please ensure the following
Python already installed and then take an image - use that image to bootstrap
You enable User-Data and pass the user-data commands during the launch time
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
the document from aws say that only shell and cloud-init directive that are supported

Categories

Resources