Running process in the background

前端 未结 5 946
日久生厌
日久生厌 2020-12-17 03:24

I am finding hard to run a process at background using paramiko. I used :

stdin, stdout, stderr = ssh.exec_command(\'executefile.py &\') 
5条回答
  •  没有蜡笔的小新
    2020-12-17 04:05

    I've tried all the methods described here and here without success, and finally realized that you need to use channels instead of using the SSHClient directly for calling exec_command (this does not work in background):

        client = paramiko.SSHClient()
        client.connect(
            ip_address, username='root', pkey=paramiko_key, timeout=5)
        client.exec_command('python script.py > /dev/null 2>&1 &')
    

    You should create and use a channel, this works in background:

        client = paramiko.SSHClient()
        client.connect(
            ip_address, username='root', pkey=paramiko_key, timeout=5)
        transport = client.get_transport()
        channel = transport.open_session()
        channel.exec_command('python script.py > /dev/null 2>&1 &')
    

    So nohup, dtach, screen, etc, are actually not necessary.

提交回复
热议问题