0

I have a python script and it will call ssh process to create remote process A. A will create a ssh subprocess to server B.

When I run the script and Ctrl-C to interrupt it, the A will close in remote side. But the ssh forked by A still executed. Here's my script:

import os
import inspect
import logging
import sys
import subprocess
import signal

CURRENT_DIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
PYTHON_PATH = '/usr/local/bin/python'
SNAPSYNC_PATH = CURRENT_DIR + '/snapsync.py'
EXECUTE_PATH = PYTHON_PATH + ' ' + SNAPSYNC_PATH


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    cmd = 'ssh -p 22 -C -oServerAliveInterval=600 -oServerAliveCountMax=3 remote_ip "a.py" '
    logging.info(cmd)
    try:
        handle = subprocess.Popen(cmd, bufsize=0, stdin=None, shell=True)

    except KeyboardInterrupt:
        print "Caught KeyboardInterrupt, terminating workers"
        handle.terminate()
        handle.kill()

I want to know how to kill the B process when I Ctrl-C to the local script?

code_worker
  • 384
  • 3
  • 16
  • possible duplicate of https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true?answertab=active#tab-top –  Oct 17 '17 at 11:45
  • @MikeOlszak but I need to kill remote subprocess, does that work? – code_worker Oct 17 '17 at 15:14

0 Answers0