Make python script to run forever on Amazon EC2 - python

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.

Related

How to run a python script always in linux (ubuntu)

I have a linux system server using ssh to connect. Now I have a python script.I want it run always.I using this commond
ubuntu:~$ nohup python3 -u ~/test/main.py > test.outs 2>&1 &
but I exit ssh connect,That python script exit at the same time.
What should I do?
You could run the script regularly / check it is running with a cronjob and this would also allow you to run the script at system start-up so it would keep running in the event of a reboot.
There are a few suggestions here;
https://superuser.com/questions/448445/run-bash-script-in-background-and-exit-terminal
Although this also suggests that nohub should stop the child process being killed when you exit the session. How are you aware that the script stops running upon exit?
There are multiple ways to do this and they depend on your use-case.
One way to do it is to install 'screen' on your server.
sudo apt-get install screen
Now, whenever you connect to your server with ssh, you can type 'screen' and then start your code there. (first time you do this, you get an explanation on screen, press space to skip it)
With this code executing, you type Ctrl + A and then Ctrl + D. This 'detaches' the screen. You can now disconnect and this 'screen' will keep existing and the code will still run.
When reconnecting, you might want to go back to this screen. Type
screen -ls
to get an overview of screens that you have running. (in this case, there'll only be one) They can be identified by the 5 numbers they all start with. So go back to this screen by typing
screen -r XXXXX
and now you're back. More information on this here:
https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/
Again, I don't know what your script does so it might not be the best solution.
You may use screen
After installation, you can start screen by sending screen to the console, and then run your script. When you type CTRL+A+D, the screen will disappear and you can exit your ssh connection.
If you re-open the screen you opened before, just type screen -r
If you have multiple screen instances, the app will show you numbers of screen when you type screen -r. You just need to find the id of the screen and type screen -r id i.e. screen -r 2643

How to run a python script on a remote server that it doesn't quit after I log off?

I've created a script for my school project that works with data. I'm quite new to working remotely on a server, so this might seem like a dumb question, but how do I execute my script named
stats.py
so that it continues executing even after I log off PuTTy? The script file is located on the server. It has to work with a lot of data, so I don't want to just try something and then few days later find out that it has exited right after I logged off.
Thank you for any help!
There are many ways you can run a python program after you disconnect from an SSH session.
1) Tmux or Screen
Tmux is a "terminal multiplexer" which enables a number of terminals to be accessed by a single one.
You start by sshing as you do, run it by typing tmux and executing it. Once you are done you can disconnect from putty and when you login back you can relog to the tmux session you left
Screen also does that you just type screen instead of tmux
2) nohup
"nohup is a POSIX command to ignore the HUP signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout."
You can run it by typing nohup <pythonprogram> &

Keep running a python script on AWS EC2 even if CLI session is closed

so I have a script running on (script is inside the instance) my AWS EC2 instance (Ubuntu), which should write some results of benchmarks into a txt file (also in the instance itself) every few hours...Now I have run the session with my terminal and ssh, but if I close my terminal or shut down my computer, the script obviously crashes.
I have even tried: "nohup myscript.py &", but without any success.
Would really appreciate some help here
Like mentioned above, start a tmux window like on your Amazon ec2 instance:
tmux new -s mywindow
Once the new window starts, run your script. Once the script is running, you can close your ssh client or shut down your local computer. When you want to see the results, log in to your ec2 through ssh again, and type this:
tmux a -t mywindow
This would take you back to your original window running your script.
In my opinion the easiest way is to install one of existing terminal multiplexers like screen or tmux and run your script inside this multiplexer. This will allow your script to run even after disconnecting from shell session.
Please keep in mind that shutting down AWS EC2 machine also kills screen/tmux session and your script won't be restored after starting that machine again. However after detaching from screen/tmux session on remote machine you can safely close or restart your local computer, reconnect after several hours or days or even years and your remote session with your script inside should still exist (as long as AWS EC2 machine wasn't restarted or your screen/tmux session wasn't closed manually be administrator for example).

Python Server in AWS

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.

to keep the script running even after internet connection goes off

I had putty on one server and run a python script available on that server. That script keep on throwing output on terminal. Later on, my internet connection went off but even then i was expecting my script to complete it job as script is on running on that server. But when internet connection resumed, I found that script has not done its job.
So is this expected ? If yes, then what to do to make sure that script runs on server even though internet connection goes off in-between?
Thanks in advance !!!
You should use screen which will let you "detach" your process from the actual terminal you're in.
On the server, you can install tmux or screen. These programs run the program in the background and enable you to open a 'window', If I use tmux:
Open tmux: tmux
Detach (run in background): press Ctrl-b d
reattach (open a 'window'): tmux attach

Categories