2

I have a python file "run.py" like below on my remote server.

import subprocess
subprocess.Popen(["nohup", "python", "/home/admin/Packet/application.py", "&"])

I want to run that file from my local computer using SSH. I'm trying like the below. However, my local terminal got stuck there. It seems it isn't being run in the background.

ssh -n -f -i /Users/aws/aws.pem admin@hello_world.com 'python /home/admin/run.py'

After running that command, my terminal got stuck.

14
  • You may wan to use en.wikipedia.org/wiki/GNU_Screen Commented Jan 6, 2016 at 13:50
  • @kfx, no, absolutely unnecessary. The problem is that the process is indeed not being sent to background Commented Jan 6, 2016 at 13:51
  • Why is it a python script? Shell would be sufficient and simpler, no? Commented Jan 6, 2016 at 13:52
  • 1
    @moeseth, have you tried simply nohup python /home/admin/Packet/application.py &? Commented Jan 6, 2016 at 13:54
  • 1
    @DavidCullen, please, remember, that it is being started by python subprocess.Popen with a list argument. Therefore NEITHER of & nor >& are special, because list argument is not passed through shell. Commented Jan 6, 2016 at 13:55

1 Answer 1

1

The following is an example I'm using, you can try something like this, customizing the ssh_options.

import subprocess
ssh_options = '-o ConnectTimeout=10 -o PasswordAuthentication=no -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no'
server_name = 'remote_server.domain'
cmd = 'ssh ' + ssh_options + ' ' + server_name + ' "/usr/bin/nohup /usr/bin/python /home/admin/run.py 2>&1 &"'  
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Later you can redirect the output to a flat file, changing :

2>&1 &

for:

>> /path/lo/log_file.txt 2>&1 &
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, this works. However, I want to execute script from remote server.
My setup is a little more complex, but what I'm doing is basically this, the remote cmd download the script and calls the interpreter to execute it "'/usr/bin/nohup /usr/bin/wget -T3 -qO- " + args.url + args.script + " | " + args.interpreter + ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.