Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continuously run a Python script on an EC2 server?

Tags:

I've setup an Amazon EC2 server. I have a Python script that is supposed to download large amounts of data from the web onto the server. I can run the script from the terminal through ssh, however very often I loose the ssh connection. When I loose the connection, the script stops.

Is there a method where I tell the script to run from terminal and when I disconnect, the script is still running on the server?

like image 309
David Avatar asked Jun 03 '11 20:06

David


People also ask

How do I make Python code run continuously?

sleep() Function To Run Script repeatedly. So, if you do not want to use the above code and just want to run your script repeatedly then you can use the time. sleep() function. This function allows your script to keep running after sleeping for a certain amount of time.

How do I run a Python script at the same time everyday?

Method 2: Using Windows Task Scheduler. ' in the Actions Tab. And give a suitable Name and Description of your task that you want to Automate and click on Next. Step 3: In the next step, you have to select at what time intervals your script should be executed. Select 'Daily' and click Next.


1 Answers

You have a few options.

  • You can add your script to cron to be run regularly.
  • You can run your script manually, and detach+background it using nohup.
  • You can run a tool such as GNU Screen, and detach your terminal and log out, only to continue where you left off later. I use this a lot.
    • For example:
      1. Log in to your machine, run: screen.
      2. Start your script and either just close your terminal or properly detach your session with: Ctrl+A, D, D.
      3. Disconnect from your terminal.
      4. Reconnect at some later time, and run screen -rD. You should see your stuff just as you left it.
  • You can also add your script to /etc/rc.d/ to be invoked on book and always be running.
like image 118
yan Avatar answered Oct 25 '22 10:10

yan