简体   繁体   中英

Running remote command in background using paramiko

When I tried to run the remote command in the background by using paramiko, I'm not able to see any output of the executed command in stdout. Please go through the below illustration.

Script which runs the remote command normally:

import paramiko
import select
import time
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('host_name', username='user_name', password='password')
COMMAND = "ls && sleep 15 && ls " #note here
stdin, stdout, stderr = client.exec_command(COMMAND, get_pty=True)

while not stdout.channel.exit_status_ready():
        time.sleep(1)
        if stdout.channel.recv_ready():
            rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
            if len(rl) > 0:
                print("{}".format(stdout.channel.recv(1024).decode("utf-8")))
else:
    print("{}".format(stdout.channel.recv(1024).decode("utf-8")))

Output:

file1.txt
file2.txt
<waits for 15 seconds>
file1.txt
file2.txt

Same script which runs the remote command in the background:

import paramiko
import select
import time
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('host_name', username='user_name', password='password')
COMMAND = "ls && sleep 15 && ls &" # here is the change
stdin, stdout, stderr = client.exec_command(COMMAND, get_pty=True)

while not stdout.channel.exit_status_ready():
        time.sleep(1)
        if stdout.channel.recv_ready():
            rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
            if len(rl) > 0:
                print("{}".format(stdout.channel.recv(1024).decode("utf-8")))
else:
    print("{}".format(stdout.channel.recv(1024).decode("utf-8")))

Output:


As you can see, there is a blank line printed on the console, and the program exits within one/two second(s). I doubt whether it actually executes sleep 15 in the background and exits after initiates it or not.

Is there anything that I need to do in case of running the remote command in the background?

I'm new to the paramiko module. Maybe you can find this question simple. Please do share your comments. It'll be a great help to beginners like me.

Thanks in advance!

If you execute a command in the background, the session ends immediately, not waiting for the command to complete.

Try this command:

ssh -t user_name@host_name "ls && sleep 15 && ls &"

It's an equivalent of your Python code. And you will see that it produces no output either. It won't even wait the 15 seconds, it closes immediately. Same as your Python code.

$ ssh -t user_name@host_name "ls && sleep 15 && ls &"
Connection to host_name closed.
$

Anyway, I do not see a point of executing a command in the background, if you then wait for the command to finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM