Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute remote python script via SSH

Tags:

I want to execute a Python script on several (15+) remote machine using SSH. After invoking the script/command I need to disconnect ssh session and keep the processes running in background for as long as they are required to.

I have used Paramiko and PySSH in past so have no problems using them again. Only thing I need to know is how to disconnect a ssh session in python (since normally local script would wait for each remote machine to complete processing before moving on).

like image 453
nka Avatar asked Nov 14 '10 23:11

nka


People also ask

How do I run a python command on a remote server?

Running commands remotely on another host from your local machinelink. Using the Paramiko module in Python, you can create an SSH connection to another host from within your application, with this connection you can send your commands to the host and retrieve the output.

How do I run a python script from server?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


2 Answers

This might work, or something similar:

ssh [email protected] nohup python scriptname.py & 

Basically, have a look at the nohup command.

like image 64
David Dean Avatar answered Sep 25 '22 13:09

David Dean


On Linux machines, you can run the script with 'at'.

echo "python scriptname.py" ¦ at now

like image 22
chnrxn Avatar answered Sep 22 '22 13:09

chnrxn