Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make python script to run forever on Amazon EC2

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

like image 300
Tengyu Liu Avatar asked Apr 19 '14 05:04

Tengyu Liu


2 Answers

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.

like image 72
Sterling Graham Avatar answered Oct 07 '22 02:10

Sterling Graham


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>
like image 18
salmanwahed Avatar answered Oct 07 '22 01:10

salmanwahed