2011-06-14 3 views
3

ssh를 사용하는 원격 컴퓨터에서 프로그램을 실행하고 ssh 연결을 종료하지 않고 SIGINT 신호를 전달하는 프로그램을 작성하려고합니다. 내가 터미널에서 실행중인 경우, 다음이 나는 다음과 같은 스크립트를 사용하여이 시도신호 잡기 및 무시

ssh -t -t host "command to run" 

같은 뭔가 쉬운 것입니다 :

#!/bin/bash 

ssh -t -t host "program [email protected]" 

내가 터미널에서 잘 작동하는지 실행할 때, 그러나 proofgeneral이이 스크립트를 실행하고 SIGINT를 보냈을 때 그것은 프로그램을 죽이는 결과를 가져옵니다 (터미널을 할당 할 수 없다고 생각하십니까?).

내가 다음 코드 혼란을 가지고 : 나는 같은 것을 사용하여 호출하고 있습니다

CTRL_C = "<CTRL-C>" 

def endpoint(proc, handler): 
    signal.signal(2, signal.SIG_IGN) 

    inp = "" 
    out = "" 
    err = "" 

    inp_from_fd = sys.stdin.fileno() 
    inp_to_fd = proc.stdin.fileno() 
    out_from_fd = proc.stdout.fileno() 
    out_to_fd = sys.stdout.fileno() 
    err_from_fd = proc.stderr.fileno() 
    err_to_fd = sys.stderr.fileno() 

    while True: 
     try: 
      ins,outs,_ = select.select([inp_from_fd, out_from_fd, err_from_fd], 
             [inp_to_fd, out_to_fd, err_to_fd], 
             []) 
      for fd in ins: 
       if fd == inp_from_fd: 
        k = os.read(fd, 1024) 
        if k == "": 
         os.close(proc.stdin.fileno()) 
         return 
        inp = inp + k 
       elif fd == out_from_fd: 
        k = os.read(fd, 1024) 
        out = out + k 
       elif fd == err_from_fd: 
        k = os.read(fd, 1024) 
        err = err + k 
       else: 
        assert False 
      for fd in outs: 
       if fd == inp_to_fd: 
        while CTRL_C in inp: 
         proc.send_signal(2) 
         p = inp.find(CTRL_C) 
         inp = inp[0:p] + inp[p+len(CTRL_C):] 
        k = os.write(fd, inp) 
        inp = inp[k:] 
       elif fd == out_to_fd: 
        k = os.write(fd, out) 
        out = out[k:] 
       elif fd == err_to_fd: 
        k = os.write(fd, err) 
        err = err[k:] 
       else: 
        assert False 
     except select.error: 
      pass 
     except KeyboardInterrupt: 
      handler() 
     except IOError, e: 
      pass 

def usage(args): 
    print "must specify --client or --server" 

if __name__ == '__main__': 
    if len(sys.argv) == 1: 
     usage(sys.argv) 
    elif sys.argv[1] == '--server': 
     proc = subprocess.Popen(sys.argv[2:], 
           stdin=subprocess.PIPE, 
           stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
     def INT(): 
      proc.stdin.write(CTRL_C) 
      proc.stdin.flush() 
     endpoint(proc, INT) 
    elif '--client' in sys.argv: 
     proc = subprocess.Popen(sys.argv[2:], 
           stdin=subprocess.PIPE, 
           stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
     import time 
     time.sleep(1) 
     def INT(): 
      pass 
     endpoint(proc, INT) 
    else: 
     usage(sys.argv) 

:

remote.py --client ssh -t -t host "remote.py --server <program-to-run> <args>" 

내가를 처리하기 위해 여기에 잘못하고 있어요 뭔가가 있나요 신호? 나는 신호 2 처리기에 인쇄물을 넣으려고 시도했지만 인쇄는하지만 ssh를 죽이는 중입니다 (콘솔에 인쇄 된 "신호 2에 의해 죽었습니다."). 파이썬은 신호를 그 자식에게 전달합니까? 이 문제를 해결할 방법이 있습니까? 이 작업을 수행하는 더 쉬운 방법이 있습니까?

모든 포인터 주셔서 감사합니다.

답변

1

os.setpgrp (setpgrp 맨 페이지 참조) 그렇지 않으면 신호가 하위에 전파됩니다.