I have written some Web services in Python.I want to deploy it in AWS, I have created the instance.
I tried to run using putty and it was coming up well using the command python Flo.py, which starts the server 0.0.0.0:8080. But the problem is when I close the putty window the server is terminating. How i can start a server in 8080 just like httpd?
All helps are invited
I highly recommend you use screen (or tmux). And you may want to use upstart as well.
Screen:
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).
tmux and screen are doing the same thing - which is terminal multiplexing. This will give you a terminal you can attach to and disconnect from to keep it running when you're not on the server.
To test it simply install using:
sudo apt-get install screen
Now use the following to open a screen terminal under the name my_screen, running your script as it starts:
screen -dmS my_screen python Flo.py
And attach to it using:
screen -r my_screen
Detach using ctrl+A followed by ctrl+D, and now you can leave the server (screen will keep running with the process in it)
Read more here.
Upstart:
Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
Upstart is the new way to start services on debian as soon as the system starts.
To add an upstart service you need to add a configuration file under /etc/init (open one of the files there and see an example).
These files can be extremely simple so don't be intimidated by what you see there.
You can make a service to run your server / service and send output to a log file which you can then use to keep track of what's happening.
Read more here.
Related
I am running a Python script that collects data and is running inside a Virtual Environment hosted remotely on a VPS (Debian based).
My PC crashed and I am trying to get back into the visual logs of the python script.
I know that the script is still running because it saves its data into a CSV file. That CSV is still being written.
If I activate the source again, then I can rerun the script. It sounds to me that I will have 2 instances of the same script running in this case...
I am not familiar with the virtual environment and I cannot find the right way to do it without deactivating and reactivating it. I am running my script on the cheapest OVH VPS I could buy because my computer is clearly not reliable for running 24/7.
You might use screen to run your script in a separate terminal session. This will avoid losing logging if the ssh connection gets dropped.
The workflow would be something in the lines of (on your host):
# Install screen
$ sudo apt udpate
$ sudo apt install screen
# Start a screen session
$ screen
# Run your script
$ python myscript.py
In case of dropping your ssh connections, it'll be enough to:
# ssh back into the host from your client
# reattach previous screen session
$ screen -r
For advanced use the official docs are quite comprehensive.
Note: As a more general note, what explained above is pretty much the basic logic of a terminal mulitplexer. You'll be able to achieve the same using tmux.
I am looking for help deploying my flash app. I've already written the app and it works well. I'm currently using the following command in the directory of my flask code:
sudo uwsgi --socket 0.0.0.0:70 --protocol=http -w AppName:app --buffer-size=32768
This is on my Amazon Lightsail instance. I have the instance linked to a static public IP, and if I navigate to the website, it works great. However, to get the command to continuously run in the background even after logging out of the Lightsail, I first start a screen command, execute the above line of code, and then detach the screen using ctrl-a-d.
The problem is, if the app crashes (which is understandable since it is very large and under development), or if the command is left running for too long, the process is killed, and it is no longer being served.
I am looking for a better method of deploying a flask app on Amazon Lightsail so that it will redeploy the app in the event of a crash without any interaction from myself.
Generally you would write your own unit file for systemd to keep your application running, auto restart when it crashes and start when you boot your instances.
There are many tutorials out there showing how to write such a unit file. Some examples:
Systemd: Service File Examples
Creating a Linux service with systemd
How to write startup script for Systemd?
You can use pm2
Starting an application with PM2 is straightforward. It will auto
discover the interpreter to run your application depending on the
script extension. This can be configurable via the Ecosystem config
file, as I will show you later on this article.
All you need to install pm2 and then
pm2 start appy.py
Great, this application will now run forever, meaning that if the process exit or throw an exception it will get automatically restarted. If you exit the console and connect again you will still be able to check the application state.
To list application managed by PM2 run:
pm2 ls
You can also check logs
pm2 logs
Keeping Processes Alive at Server Reboot
If you want to keep your application online across unexpected (or expected) server restart, you will want to setup init script to tell your system to boot PM2 and your applications.
It’s really simple with PM2, just run this command (without sudo):
pm2 startup
Pm2 Manage-Python-Processes
I have created a Django application and uploaded in to AWS EC2. I can access the site using public IP address only when I run the python manage.py in AWS command line.
If I close the Putty window, I am not able to access the site. How can I make sure that the site is available always even if I close the command line / putty?
I tried WSGI option but its not working at all. Appreciate your help to give us a solution to run the Python application in AWS.
It happens because you are running the app from within the SSH session, which means that ending the session (SIGHUP) will kill your application.
There are several ways to keep the app running after you disconnect the SSH, the simplest would be to run it inside a screen session and keeping this instance running while disconnecting from SSH, the advantage of this method is that you can still control the app when you are reconnecting to this machine and control the state of the app and also potentially see the logs.
Although it might be pretty cool it's considered a patch, the more stable and solid way would be to create a service that will run the app and will allow you to start, stop and look at logs using the nifty wrappers of systemd.
Keep the process running with screen:
First you'll have to make sure screen is installed (apt-get or yum) whatever suits your desired distro.
Run screen.
Run the app just like you did outside screen.
Detach from the screen session by pressing Ctrl+A and then d.
Disconnect from the SSH and see how the service is still running.
Creating a systemd service is a bit more complicated so try and read through the following manual.
I have a flask web server running on the cloud and the process goes down if I close the terminal or if my local machine loses network connection. So how can I make sure that my flask process runs forever with out any dependency on the terminal? And is there any way to monitor the process. Like for example if I log in again and go to a URL I get all the details I need.
You can use screen command. Let's assume that your application is app.py and the steps are as follows.
1.Create a new session and it will write screen output to a file named screenlog.0.
screen -L
2.Now you are in a session and you can run your application.
python app.py
3.Detach this session.
Press "Ctrl-A" and then press "D" on your keyboard to detach the session.
Then you will return your normal terminal. Your application is running in the background in a virtual screen and all screen output will be written to the file screenlog.0. You can close your terminal without affecting your application.
4.Reattach to the previous session.
If you want to shut down your application or to do some other things, you can use command screen -r to reattach to the previous session.
5.Terminate a session.
After attaching to a session, just use exit to terminate it.
For more details, please read this tutorial.
I have a python script that basically runs forever and checks a webpage every second and notifies me if any value changes. I placed it on an AWS EC2 instance and ran it through ssh. The script was running fine when I checked after half an hour or so after I started it.
The problem is that after a few hours when I checked again, the ssh had closed. When I logged back in, there was no program running. I checked all running processes and nothing was running.
Can anyone teach me how to make it run forever (or until I stop it) on AWS EC2 instances? Thanks a lot.
Edit: I used the Java SSH Client provided by AWS to run the script
You can use Linux screen.Linux screen tool can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session.
To install:
sudo apt-get install screen
Start a new session:
screen -S <screen_name>
Run your process as you run it in the screen session. If you want to back to your main terminal press key shortcut ctrl+a+d. And also view the screen by typing,
screen -r <screen_name>
You can run the program using the nohup command, so that even when the SSH session closes your program continues running.
Eg: nohup python yourscriptname.py &
For more info you can check the man page for it using
man nohup.