-1

I would like to connect a remote machine and run background script in that machine from python.

I tried:

os.system("ssh root@10.0.0.1 \' nohup script.sh & \')

But it seems not working. And if I put nohup in script.sh, and simply run

os.system("ssh root@10.0.0.1 \' script.sh \'")

The nohup command would not work in either cases.

I'm confused why so, and is there anybody knows how to do background job from python or it's just impossible doing it this way?

Graham
  • 7,431
  • 18
  • 59
  • 84

2 Answers2

0

What kind of errors are you getting? What version of Python are you using?

You should take a look at this Python subprocess - run multiple shell commands over SSH

import subprocess
sshProcess = subprocess.Popen(["ssh", "root@10.0.0.1"],
                               stdin=subprocess.PIPE, 
                               stdout = subprocess.PIPE,
                               universal_newlines=True,
                               bufsize=0)
sshProcess.stdin.write("nohup script.sh &")
Payman
  • 2,630
  • 1
  • 12
  • 18
  • Thanks for answering. I'm using Python 2.7.10, and subprocess doesn't work neither. I didn't get error message at all, it's strange that I couldn't event get a 'nohup.out' file. – Jasmine Ding Oct 11 '18 at 08:13
0

For example you have a local script (python, bash, etc. Here I am demonstrating you using a python script)

First you create a python file locally. Lets say hello.py

# 'hello.py'
import os
print os.system('hostname')

Secondly now a python script which would execute the above hello.py on a remote machine

import pathos
copy = pathos.core.copy('hello.py', destination='abc.remote.com:~/hello.py')
exec = pathos.core.execute('python hello.py', host='.remote.com')
print exec.response()