How to continuously run a Python script on an EC2 server?
Question:
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?
Answers:
If it just a utility you run ad-hoc, not a service daemon of some kind, i would just run it in screen
. Than you can disconnect if you want and open the terminal back up later… Or reconnect the terminal if you get disconnected. It should be in your linux distros package manager. Just search for screen
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:
- Log in to your machine, run:
screen
.
- Start your script and either just close your terminal or properly detach your session with: Ctrl+A, D, D.
- Disconnect from your terminal.
- 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.
You can also use nohup
to make your script run in the background or when you have disconnected from your session:
nohup script.py &
The &
at the end of the command explicitly tells nohup
to run your script in the background.
nohup
runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.
Syntax:
nohup Command [Arg]...
Example:
nohup example.py
nohup rasa run
Also, you can run scripts continuously using the cron
command.
For more:
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?
If it just a utility you run ad-hoc, not a service daemon of some kind, i would just run it in screen
. Than you can disconnect if you want and open the terminal back up later… Or reconnect the terminal if you get disconnected. It should be in your linux distros package manager. Just search for screen
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:
- Log in to your machine, run:
screen
. - Start your script and either just close your terminal or properly detach your session with: Ctrl+A, D, D.
- Disconnect from your terminal.
- Reconnect at some later time, and run
screen -rD
. You should see your stuff just as you left it.
- Log in to your machine, run:
- For example:
- You can also add your script to
/etc/rc.d/
to be invoked on book and always be running.
You can also use nohup
to make your script run in the background or when you have disconnected from your session:
nohup script.py &
The &
at the end of the command explicitly tells nohup
to run your script in the background.
nohup
runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.
Syntax:
nohup Command [Arg]...
Example:
nohup example.py
nohup rasa run
Also, you can run scripts continuously using the cron
command.
For more: