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?