Paramiko过程提前结束

2024-06-01 01:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在多台计算机上运行一个命令,但是这个命令几乎立即开始然后结束。它需要一段时间来运行,所以我只想启动,然后转到下一台计算机。在

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
un = 'username'
pw = 'password'
version = None
count = 0

for i in xrange(1):
    host = 'hydra' + str(i) + '.eecs.utk.edu'

    try:
        conn = client.connect(host, username=un, password=pw)
    except:
        continue
    count += 1
    stdin, stdout, stderr = client.exec_command('ls ./neuro/networks/anets/')

    #gets highest numbered folder to compute next version
    if version == None:
        version = max(map(int, stdout.read().split())) + 1

    anew = 'mkdir ~/neuro/networks/anets/{}'.format(version)
    bnew = 'mkdir ~/neuro/networks/bnets/{}'.format(version)

    #this is the command that ends too early
    net = 'nice -n 19 ~/neuro/apps/polebalance/bin/PBEO > ~/neuro/networks/anets/{v}/net{pc:02d}_{v}.txt'.format(v=version, pc=i)
    print '.../anets/{v}/net{pc:02d}_{v}.txt'.format(v=version, pc=i)

    stdin, stdout, stderr = client.exec_command(anew)
    stdin, stdout, stderr = client.exec_command(bnew)
    stdin, stdout, stderr = client.exec_command(net)

    client.close()
print 'making {} nets'.format(count)

有人知道如何启动进程,然后在进程不终止的情况下终止会话吗?在


Tags: clientformathostparamikonetversionstderrstdin
1条回答
网友
1楼 · 发布于 2024-06-01 01:33:27

您可以使用nohup运行命令来禁用挂断信号,并以&结束该命令以在后台运行。现在您的ssh可以退出并继续运行。创建目录时,您应该等待exec_command完成,以防系统有一段时间运行缓慢。我把这两个品牌合并成一个,所以只有一个等待。除此之外,我只在net命令中添加了nohup和{}。在

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
un = 'username'
pw = 'password'
version = None
count = 0

for i in xrange(1):
    host = 'hydra' + str(i) + '.eecs.utk.edu'

    try:
        conn = client.connect(host, username=un, password=pw)
    except:
        continue
    count += 1
    stdin, stdout, stderr = client.exec_command('ls ./neuro/networks/anets/')

    #gets highest numbered folder to compute next version
    if version == None:
        version = max(map(int, stdout.read().split())) + 1

    anew = 'mkdir ~/neuro/networks/anets/{}'.format(version)
    bnew = 'mkdir ~/neuro/networks/bnets/{}'.format(version)

    #this is the command that ends too early
    net = 'nohup nice -n 19 ~/neuro/apps/polebalance/bin/PBEO > ~/neuro/networks/anets/{v}/net{pc:02d}_{v}.txt &'.format(v=version, pc=i)
    print '.../anets/{v}/net{pc:02d}_{v}.txt'.format(v=version, pc=i)

    stdin, stdout, stderr = client.exec_command(';'.join((anew, bnew)))
    # wait directory creation done
    stdout.read()
    # cmd in background with nohup so won't exit on ssh exit
    client.exec_command(net)
    client.close()

print 'making {} nets'.format(count)

更新

这个测试程序通过ssh在本地计算机上运行。每个远程命令都写入日志文件,我们读取该文件以确保其正常工作。在

^{pr2}$

在我的机器上结果是

$ python3 test.py
Run remote execution test via ssh on local computer
Password: 
running nohup /usr/bin/python3 /home/td/tmp/test.py /home/td/tmp/27212-0.text &
running nohup /usr/bin/python3 /home/td/tmp/test.py /home/td/tmp/27212-1.text &
running nohup /usr/bin/python3 /home/td/tmp/test.py /home/td/tmp/27212-2.text &
verify all started
Sun Apr  9 13:05:30 2017: Proof of life, pid 27232
Sun Apr  9 13:05:31 2017: Proof of life, pid 27255
Sun Apr  9 13:05:31 2017: Proof of life, pid 27275
verify all ended
10..9..8..7..6..5..4..3..2..1..
Sun Apr  9 13:05:30 2017: Proof of life, pid 27232
Sun Apr  9 13:05:40 2017: done
Sun Apr  9 13:05:31 2017: Proof of life, pid 27255
Sun Apr  9 13:05:41 2017: done
Sun Apr  9 13:05:31 2017: Proof of life, pid 27275
Sun Apr  9 13:05:41 2017: done

相关问题 更多 >