How to keep web.py app running always? - python

I am running web py like below from terminal.
$ python app.py 8080
now how do i keep it running even when I close the terminal?
Is there any way to keep this server running.
Thank you

you can use nohup for that.
$ nohup python app.py 8080 &
It will stay open even if you close the terminal. You'll need to use kill and send some signal to it to close it.

I think that this is a question belonging to another stackexchange site (serverfault?).
Anyway, the answer is: run it in a screen:
screen python app.py 8080
Detach from the screen using the key combination:
Ctrl+a+d
Attach to the screen again (to see ouput from your process)
screen -r
Quit screen:
Ctrl+c

You can use supervisord. It is intended to do such tasks (and much more). Documentation is pretty clear, so it should not be a problem to set up it. See how to run supervisord.
Also do not use nohup since it will not do anything like autorestarting server after failure or after reboot.

In a Bourne-shell compatible shell (Bash, Bourne, Korn):
python app.py 8080 &
I just see that you wrote that you want to close the terminal. Apparently my answer won't work then.

Related

Pycharm debugger "waiting for connections" but running still working

I am trying to debug django project with docker-compose interpreter.
Here is my pycharm configurations
But when I trying to debug it project still running but debugger is still waiting for connection and break point not working
I think that structure of my project have problem cause i'm try to debug other project it still working.
Here is my project structure
What am i doing wrong?
To whomever else it might help, the problem in my case was that I attempted to use the debugger coupled with a run inside a Docker container functionality.
I also happened to have all ports published on that container which prevented the debugger to connect. Publishing only ports I actually needed, resolved the problem.
Check the running ports on your machine. In my case, the port that PyCharm wanted to use for debugging (127.0.0.1:xxxx) was being used by another program on my laptop.
You can check the running ports using the following command on mac:
lsof -i -P | grep -i "listen"
Or, the following command once you know which port PyCharm is trying to use (usually you can see that on top of the console tab of PyCharm after starting debugging process):
sudo lsof -i :xxxxx
After running that, you should see a list with PID numbers, names of program etc. Then you can kill the running process on that port using the PID:
sudo kill -9 PID
Or, just restart your computer.
If that doesn't work, then it might be due to using already-existing Python module names. Make sure the names of the Python files in your project isn't the same as any other library/code from python.

Git bash "Watching for file changes with StatReloader" stuck and never loads

I have setup a Django project on a virtual environment on my PC. When using the command
python manage.py runserver 0.0.0.0:8000
Bit Bash stops doing anything and I have to end the program to start over. I have waited several minutes and when I end the session, a dialogue says:
Processes are running in session:
WPID PID COMMAND
14904 1534 c:\Users\mine\AppData\Loca
Close anyway?
I have looked at every related question to this and tried every solution but I cannot get this to work, on or off the virtual environment.
Not sure if this applies, but I also noticed that in my task manager, python3.9.exe appears twice when trying to start the server. The status says running, and the PIDs are different numbers.
This is because something already running on port 8000. You can run the Django server by hosting on another port like 8080 but, you can also kill the already running task on the port. Follow the link to know how exactly you can kill any running process.
It is highly likely that another application is running using the port 8000. Try running the server using another port, say 8088 and share if the same issue persists.
To run on specific port kill all pids which is showing you on Git bash
(ps -ef)... kill them using (kill -9 pid_no)... then run your runserver command.
Ex:
ps -ef
kill -9 123
I ended up paying someone to fix this. Here is the conversation I had with the person who found the issue. Hope this helps someone out there.
Also, I noticed sometimes it still does freeze up on the watching for file changes with StateReloader until I go the browser and type in http://localhost:8000/. Once I do that the rest of the text loads and my browser shows The install worked successfully! Congratulations!
it works, what did you do?
- We have existing processes on port 8000.I think, you had the server running and kept trying over and over. So, I killed process using 8000 port.
- Next, I run Django project again.
- So, now it's working well.
ok. Can you tell me what you did at the start to make it work? Or was it always working?
- I started git bash session again when I started. Maybe git bash session had problems.
But this was going on for a day, I restarted the computer several times and restarted git bash. Same issue.
You must have done something.
- Sorry, I didn't do anything except restart git bash
how did you restart it?
- right click top of window, select NEW
How do I turn server off?
- press Ctrl + C
- And you didn't quit the server exactly before. Maybe you closed git bash directly and server was still live
- So, we had many 8000 ports.
Ok. How can I see what is on that port now?
- You can use this cli
- netstat -ano | findstr :<PORT>
- We don't have running 8000 port now.
I have never found solutions to this problem even with killing ports, changing port etc.
It never display the following part:
Starting development server at http://127.0.0.1:8000/
However, when I use the Git Bash terminal include into vscode, it displays the url.
Terminal > New Terminal > then:
Result :
python manage.py runserver 127.0.0.1:8080

How can I keep a python script on a remote server running after closing out of SSH?

I've coded a stock trading bot in Python3. I have it hosted on a server (Ubuntu 18.10) that I use iTerm to SSH into. Wondering how to keep the script actively running so that when I exit out of my session it won't kill the active process.
Basically, I want to SSH into my server, start the script then close out and come back into it when the days over to stop the process.
You could use nohup and add & at the end of your command to safely exit you session without killing original process. For example if your script name is script.py:
nohup python3 script.py &
Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (kill -SIGHUP <pid>). This can be prevented using nohup, as it catches the signal and ignores it so that it never reaches the actual application.
You can use screen
sudo apt-get install screen
screen
./run-my-script
Ctrl-A then D to get out of your screen
From there you will be able to close out your ssh terminal. Come back later and run
screen -ls
screen -r $screen_running
The screen running is usually the first 5 digits you see after you've listed all the screens. You can see if you're script is still running or if you've added logging you can see where in the process you are.
Using tmux is a good option. Alternatively you could run the command with an & at the end which lets it run in the background.
https://tmuxcheatsheet.com/
I came here for finding nohup python3 script.py &
Right solution for this thread is screen OR tmux. :)

How do I keep a Python application running in an EC2 instance?

I have a python application which I uploaded to an EC2 instance as a file named "example.py". I want this code to be running continuously, as it has a while loop that calls a 3rd party Twitter API every minute. If I run "python3 example.py", and close the terminal, the SSH session terminates. Will the python application continue running in that EC2 instance? How can I make that sure? Thanks.
Quick Solution - You run in SSH terminal:
nohup python3 example.py &
(best if you don't wanna see what is going on later)
or
Best Solution - You create a Screen in SSH terminal:
screen
Then you press ctrl+a+d to detach from it. Then you can close SSH safely. If you want to see what is going on later, you can just:
screen -r
You can use the nohup command to disconnect from the current session and & to run in background:
nohup python3 example.py &

How do I restart a python script running inside of a screen using a bash script?

Okay here's my situation:
On a server I have a screen session (which was initiated using screen -S python-script) running a python script inside of it.
I make changes to this project by pushing to a git repo. When I want to have the changes reflected on the project, I ssh into the server, screen -x python-script and then Ctrl-c to send keyboard interrupt, then git pull origin master and then ./run_script.py
I want to do something clever and make my project able to update and restart itself. Another small python server would live on this remote server and listen for a request from my project, this python server would then use subprocess to run a bash script which would update and restart my project.
It's a bit convoluted I know, and I'd really like some suggestions if there's a better way to do what I'm wanting.
Right now if I could formulate a bash script to do what I'm looking for, then I know this would work (probably..).
1) Use nohup instead of screen, redirect stdout to a rotative log if you're interested in the outputs. Store the PID somewhere.
2) Set a 'deployment' branch on your repo.
3) Set a cronjob on the server to pull 'deployment' branch each x minutes.
4) Use a githook to trigger an start/stop each time there's a new commit on your 'deployment' branch. Use the stored pid to kill it.
EDIT:
If you need to kill via Ctrl + C, just send SIGINT with kill then wait for the pid to die.

Categories

Resources