Django, how to run a command through Windows Task Scheduler - python

How to schedule and run a specific command in django using Windows Task Scheduler. My django project is not currently local server deployed but using the manual set up just like activating the virtual environment and then typing the python manage.py runserver on terminal rather deploying through xampp or laragon. But i am bit confused on how to achieve to schedule and run a command like python manage py get_source through the use of Windows Task Scheduler.

Don't know if you're still looking for this but I found a working solution here - configure .bat file to run commands in Django Virtual Env
Very simple; point to the location of your project directory...
cd C:\webapps\my-project-dir-with-manage.py-inside
copy and paste the contents of the system generated activate.bat,found inside your venv/Scripts folder...
add your command line...
.\manage.py <your command here>
save as myfile.bat, and schedule via the Windows Task Scheduler.
Super simple.

I had the same problem.
The main issue is, that you missed the full path to python.exe as an execution application. "Python" will not work.
And then your application as an argument.
Additionally, to that, you can add w to py. This will make it runnable on windows without a .bat file.
Application to execute:
C:\user\python.exe Argument: manage.pyw runserver

Related

Why can't I run "python manage.py startup APP_NAME" with django?

I'm trying to build a Django app, but it doesn't let me startup the app so I can bring it to the development server. I run python manage.py startup APP_NAME, where I expect the app to run. Instead, I get an error saying there is no such file or directory called manage.py. However, I have it open.
Is there a reason for this?
Perhaps what you need to ensure on your terminal what is the directory your project is located, by using command prompt "ls" if you are using Linux, or dir on Windows, then you need to activate your virtual environment using source virtual_env_name/bin/activate... then you can execute python manage.py
You need to run:
python manage.py startapp APP_NAME
to create new app in django

Run DJango server from outside of virtual environment by using script

I have created DJango Project inside virtual environment.
Let's say virtual environment DJangoProject project name is Mysite
I am tired of running ./manage.py runserver everytime so that I wanted to automate running server when I was logged into ubuntu.I tried many ways but was always failing to access the path of virtual environment. So help me to run manage.py runserver from outside of virtual environment using bash or shell scripting.
any help can be appreciate!
What are looking for is something like Gunicorn, because runserver is only for development purposes. This is detailed tutorial for you to use
Link
You can't "run manage.py runserver from outside of virtual environment" if your project relies on a virtual environment... But nothing prevents you from writing a wrapper bash script that cd into your project's root, activate the virtualenv and launch the dev server.
This being said, I really don't see the point - and I even see a couple reasons to not do so, the first one being that you definitly want to keep the terminal where you run your dev server from opened so you can read all the logs in real time.
That's easy in Ubuntu you just need to create a shell script and run it from it's folder in the terminal as : ./shellscriptname.sh
but for that first you need to create the shell script by writing the same commands used on terminal to run the django server.
Example:
echo Hello Sangeeth
echo Lets start the app
cd myapp
cd venv
source bin/activate
cd ..
cd myapp
python manage.py runserver
even though this method helps you run the app , you wont be seeing the actual folder access and virtual environment on the terminal.

Local Django server cannot connect when run through Bash on Windows

I recently realized that I could use Bash for Windows as the terminal for use in Pycharm so that the IDE had a proper terminal in it even through it was in Windows. I want to work on a Django project but I realized something strange. When I run the server through the IDE's run button, everything runs as expected. However, when I try to run the server through the command line via
python3 manage.py runserver,
the terminal output is the same, but my browsers cannot connect. Instead they say that the connection was reset. I have full installations of Django and Python for both Windows and the Bash environment and both seem to be fully functional. I can make migrations and create new apps through the command line just fine, but I was wondering if anybody else knew why the Django Server does not connect when run through Bash on Windows

How to run Python server without command prompt

I'm developing some Python project with Django. When we render the Python/Django application, we need to open the command prompt and type in python manage.py runserver. That's ok on for the development server. But for production, it looks funny. Is there anyway to run the Python/Django project without opening the command prompt?
The deployment section of the documentation details steps to configure servers to run django in production.
runserver is to be used strictly for development and should never be used in production.
You run the runserver command only when you develop. After you deploy, the client does not need to run python manage.py runserver command. Calling the url will execute the required view. So it need not be a concern
If you are using Linux I wrote a pretty, pretty basic script, which I am always using when I don't want to call this command.
Note: You really just should use the "runserver" for developing :)
#!/bin/bash
#Of course change "IP-Address" to your current IP-Address and the "Port" to your port.
#ifconfig to get your IP-Address
python manage.py runserver IP-Address:Port
just name it runserver.sh and execute it like this in your terminal:
./runserver.sh

Django Apache - Run script as Root

My django project calls a python file at a scheduled time using "at" scheduler. This is executed within my models.py
command = 'echo "python /path/to/script.py params" | /usr/bin/at -t [time] &> path/to/at.log'
status = os.system(command)
Where [time] is schedule time.
It works perfectly when I run it within Django Dev server (I usually run as root but it also works with other users as well)
But when I deployed my application on Apache using mod_wsgi, it doesn't work. at logs shows that the job was schedule but it doesn't execute it.
I tried everything from changing the ownership to www-data, permissions, made it into executable to all users, to setuid to root (Huge Security Issue)
The last thing I want to do is run apache as root user.
Use cron or celery for scheduled tasks. If you need to run something as root, it'd make sense to re-write your script as a simple daemon and run that as root, you can pass commands to it pretty easily with zeromq.

Categories