Run program on server forever without manually starting it - django

I have created an application which resides on a server. The application uses Django to connect. So, if I want to access the web page I have to run the following command to start the server -
python manage.py runserver ip adress:port number
What is the way to keep it running all the time even after shutting down my computer?
But, I also want to save the logs of the application so that I can see it later and debug or just check the running of the program whenever I want to.

I managed to solve the issue by running the following command -
nohup python manage.py runserver ip:port > Output.txt &
The log is getting saved in the Output.txt file.

Related

Unable to create super user in django on my online server

Inside the cpanel -> python app i have tried several time to create super user. when I tried to execute this commad inside Execute python script
manage.py createsuperuser
then it will return this error
Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.
How to solve this problem, or any manuall solution, i found several solution but all the solution for local server.
There is no difference between creating superuser on local server and production server. You have to do next:
Enter your server via ssh.
Go to your project root folder (with manage.py file)
Type python manage.py createsuperuser (use your virtual environment or system interpreter, depends on).

Django: Run Constantly Running Background Task On IIS-Hosted Application

I have a Django web application hosted on IIS. I subprocess should ALWAYS BE running alongside the web application. When I run the application locally using
python manage.py runserver
the background task runs perfectly while the application is running. However, hosted on IIS the background task does not appear to run. How do I make the task run even when hosted on IIS?
In the manage.py file of Django I have the following code:
def run_background():
return subprocess.Popen(["python", "background.py"], creationflag=subprocess.CREATE_NEW_PROCESS_GROUP)
run_background()
execute_from_command_line(sys.argv)
I do not know how to resolve this issue.
Would something like Celery work to indefinitely run a task? How would I do this? Please give step by step instructions.
You could set appplication set to auto-start by following below steps:
Select Site -> advance setting->Preload enable=”true”
Select Application pool->advance setting->start mode=”always running”, Under the Process Model section, set the Idle Time-out (minutes) option to 0 and Under the Recycling section, set the Regular Time Interval (minutes) option to 0
Run iisreset command from the command prompt.
Also, check that you set FastCGI setting:
Regards,
Jalpa.

jenkins start django server after successful build

We use jenkins as continious integration system. We have two django servers validated by jenkins.
jenkins validates successully the first server. The second server depends on the first one. Thus we would like to launch at the end of the first server validation the first server itself.
We are using python, virtualenv and django and defined the Virtualenv Builder as follow:
pip install -r requirements.txt
rm -f .coverage
fab localhost test
coverage xml
nohup python manage.py runserver 9090 &
The issue is that the build never ends due to the nohup.
How can I launch the server after a successful build?
I had the same problem.
Ken,
I tried using fabric, but again python manage.py runserver - runs continuosly, so the next command is not starting.
And just few mins ago my collegue showed me how to use nohup and with variable BUILD_ID of Jenkins it would be like this to get Success from the build and leave the Django server running:
BUILD_ID=dontKillMe nohup python manage.py runserver host_server &
This worked for our Django project testing.
Since you are using fabric to test, I would recommend defining another fabric task, say, deploy, which you could call assuming the build succeeds.
Much like the call to fab completes for a successful build such that you get to the nohup line, I would expect the deploy task to return also.
You may also want to consider making the server a service (either via an /etc/init.d style script, or upstart if Ubuntu), and have the fabric task stop the currently running one, copy over whatever new files it needs (or similar process), and then restart it.
Assuming what you have above is a bash script or similar, you may want to also define set -e so that, in case any of the commands returns a non-success code, the script will fail, and in turn, fail the build.

Execute command inside django

I've been working with the PaaS service AppFog and I was able to get my Django up and running, but the problem is that my application uses static files and this are not working because it needs to execute the collectstatic command in shell.
I've been reading about it on the internet but I wasn't able to find a proper solution. Should I make a shell file and execute it? How?
I appreciate your time.
You can execute collectstatic with the following command:
python manage.py collectstatic
This is called an administrative management command. You can find out more about these and implementing them yourself by reading https://docs.djangoproject.com/en/1.4/howto/custom-management-commands/.

Read/Write permission for a program run with subprocess?

I've got a Django app, which calls a program using subprocess.call(). This program creates a couple of files, which I then use back in my app. The problem is the program doesn't seem to have permission to create files. I also tried calls like subprocess.call(['mkdir','/tmp/myapp']) but the directory was not created. What do I need to do?
This is just with my dev server at the moment, which I invoke with
sudo python manage.py runserver 0.0.0.0:8080
I can run the command from the terminal manually fine.
I can also use subprocess to touch the files, which it does fine, but when it runs the program that accesses the files itself, that program throws an error because it cannot.
The /tmp/myapp directory needs to be created first. If your view function in Django doesn't show you an error it means that you are not catching it properly.
Try something like this:
response = HttpResponse()
calltxt = subprocess.call(['mkdir','/tmp/myapp'])
response.content = calltxt
return response
Append the function to your urls.py and load it in the browser.
0 means the command executed successfully, 1 means error (this is similar to the $! variable in Bash).
I think it's not the case but if your Django app is deployed in the Web Server the user that creates files and executes commands out of the Python environment is the one that owns the web server daemon. For instance www-data in Apache.
Thus, it is needed to change permissions for this user also in order to write and execute commands out of their normal scope.