how to restart a django app through fcgi - python

we run a django app on our server through this command.
python manage.py runfcgi host=127.0.0.1 port=8070 pidfile=/home/ubuntu/autoleg_webapp/autoleg_clients.pid --settings=PROD_Settings
I have made some changes to PROD_Settings.py file,i want to restart now, how would i do that?

Tour processe's PID is in the pidfile, so you should basically kill this PID.
To do so, execute the following in your shell:
kill `cat /home/ubuntu/autoleg_webapp/autoleg_clients.pid`
Thanks to the backticks, your shell will replace cat home/ubuntu/autoleg_webapp/autoleg_clients.pid with the PID that's in the file.

Related

Run flask application on linux server with nohup

I am trying to run my flask application on my linux server using nohup , but each time i run my flask application with nohup , my server will require me to kill with cntrl + c in order for me to do other things
assuming i have 2 file in my server
path(home/app/)
flask_app.py [ which is my flask application ]
flk.sh
inside my run.sh i have included
nohup python /home/app/flask_app.py
when i run my shell script in my server which is sh flk.sh
my system will hang as per shown in below image if i dont exit it as its running and all activity will go inside nohup.out
how can i run nohup on my flask application without me having to use cntrl + c to exit to run other commands.
You usually want to use nohup command arg1 arg2 &. You're just missing the ampersand.
Briefly, nohup prevents the OS from killing your job, & sends the job in the background, so it isn't tied to your current shell.

pkill -f not working from inside shell script

I have a shell script called kill.sh that helps me restart a python script I've written. I normally use pkill -f main.py to kill my forever-running python script. However, when I wrote it into a shell script it does not work.
My script
pkill -f main.py
ps aux | grep main.py # Still shows the process running.
While just executing pkill -f main.py in bash command line works as expected. Why is this?
This is not a satisfactory answer, as I cannot find out the root cause of why pkill -f does not work in a script. I ended up using a systemd Service file to manage my python process. Here's an example fyi.
[Unit]
Description=Service Name
[Service]
Environment=PYTHONUNBUFFERED=1
ExecStart=/path/to/python /path/to/python/script.py
Restart=on-failure
RestartSec=5s
WorkingDirectory=/python/project/dir/
Name the file main.service and place it in /lib/systemd/system/
Running the service systemctl start main.service
Stop the service systemctl stop main.service
Restart the service systemctl restart main.service
Show status and output systemctl status main.service -l
Now I don't have to worry about multiple processes running. If the program dies it'll even restart.

Django: Make the server continue to run without using runserver

I have started using django_channels on my server. But if I want the websocket to work, I have to use:
python manage.py runserver 0.0.0.0:8080
And the server runs and I can connect ws://myip:8080
But, as soon as I do ctrl+c. It quits i.e, I am unable to connect on ws://myip:8080 anymore.
I want to be running continuously.
You can keep it running in background without hanging up.
Just use below command.
nohup python manage.py runserver 0.0.0.0:8080 &
To kill
Find pid running on port 8080
netstat -nlp | grep :8080
and then after you get pid
kill pidnumber

django development server, how to stop it when it run in background?

I use a Cloud server to test my django small project, I type in manage.py runserver and then I log out my cloud server, I can visit my site normally, but when I reload my cloud server, I don't know how to stop the development server, I had to kill the process to stop it, is there anyway to stop the development?
The answer is findable via Google -- and answered in other forums. Example solution is available on the Unix & Linux StackExchange site.
To be explicit, you could do:
ps auxw | grep runserver
This will return the process and its respective PID, such as:
de 7956 1.8 0.6 540204 55212 ? Sl 13:27 0:09 /home/de/Development/sampleproject/bin/python ./manage.py runserver
In this particular case, the PID is 7956. Now just run this to stop it:
kill 7956
And to be clear / address some of the comments, you have to do it this way because you're running the development server in the background (the & in your command). That's why there is no "built-in" Django stop option...
One liner..
pkill -f runserver
Try this
lsof -t -i tcp:8000 | xargs kill -9
well it seems that it's a bug that django hadn't provided a command to stop the development server . I thought it have one before~~~~~
Ctrl+c should work. If it doesn't Ctrl+/ will force kill the process.
As far as i know ctrl+c or kill process is only ways to do that on remote machine.
If you will use Gunicorn server or somethink similar you will be able to do that using Supervisor.
We can use the following command.
-> netstat -ntlp
then we will get number of process running with PID, find our python server PID and Kill process.
-> kill -9 PID
For example:
From task manager you can end the python tasks that are running.
Now run python manage.py runserver from your project directory and it will work.
This worked for me on windows.
Use the below command to list all connections and listening ports (-a) along with their PID (-o).
netstat -a -o
Find the PID of the process
Then use this to kill the process
taskkill /PID PUT_THE_PID_HERE /F
Programmatically using a .bat script in Command Prompt in Windows:
#ECHO OFF
SET /A port=8000
FOR /F "tokens=5" %%T IN ('netstat -ano ^| findstr :%port%') DO (
SET /A processid=%%T
TASKKILL /PID %%T /F
)
gives
SUCCESS: The process with PID 5104 has been terminated.
You can Quit the server by hitting CTRL-BREAK.

How to have a Python Script run 24/7

I have a python script called post.py that checks for HTTP "POST" methods to my server. This is all being held on an AWS EC2 instance. I want it so that the service is constantly running this python script and that I don't have to open a command line prompt and run: python post.py
How do you set up a python script like this?
You should be using supervisord to daemonize your script. Your config file should look something like this:
[program:post]
command: /usr/bin/python -m post
directory: /home/ubuntu/post # if post.py is in a folder called post that lives in home/ubuntu
autostart: true
I found out how to daemonize my script very easily:
I went to /etc/init/ and added a file called post.config.
I put in this:
start on runlevel [2345]
stop on runlevel [!2345]
env AN_ENVIRONMENTAL_VARIABLE=i-want-to-set
respawn
exec /home/ubuntu/Files/mysite/post.py
And now it is working perfectly!
If you meant to detach the execution from terminal you can use nohup(http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html) for that,else if you wanted to execute the post.py more than once in a scheduled fashion. you can use cron job for this - linux utilty.If you want to do this in python you can check this out https://docs.python.org/2/library/sched.html

Categories