Vuk
Vuk

Reputation: 11

Python Terminating a process over ssh

I am trying to terminate a process over ssh. Now, when terminating the process directly, see commented out line in init, the process gets terminated on the remote Host. Of course, I don't want to immediately terminate, instead terminate all remote processes at the end.

Depending on the option '-t' in subprocess.Popen, different outcomes appear when I try to terminate in the loop at the end of the example:

If '-t' is not set, the process is not terminated.

If '-t' is set, the process is terminated, but my terminal hides any input and any enter moves the input line around.

Now, why doesn't, without the '-t' option, the process terminate at the loop at the end?

serverThreads = []
serverscripts = []
serverscripts.append("script1")
serverscripts.append("script2")

class ServerCaller(threading.Thread):
def __init__(self, script):
    threading.Thread.__init__(self)
    self.script = script
    self.ssh = subprocess.Popen(["ssh", '-t' , username + dataServer, self.script])
    #if I terminate here without '-t' the process on the remote Host is terminated
    #self.term()
def term(self):
    self.ssh.terminate()

for ss in serverscripts:
    serverThreads.append(ServerCaller(ss))

#if I terminate here without '-t' the process on the remote Host is NOT terminated
#if I terminate here with '-t' the process on the remote Host is terminated,
#but my terminal is screwed up
for t in serverThreads:
    t.term()

Upvotes: 1

Views: 1455

Answers (1)

Kenster
Kenster

Reputation: 25439

When you use ssh to invoke a command on a remote system, and you use -t to request a TTY for the remote command, then the remote ssh server will allocate a TTY and launch the remote command with the TTY as its controlling TTY.

When you use ssh to invoke a remote command without a TTY, then the remote SSH server will create a set of pipes and launch the remote command with those pipes as the command's standard input, output, and error.

When you terminate your local ssh instance, this closes the SSH connection to the server. When this happens, the remote server doesn't explicitly try to kill the remote process that you previously started. The remote server just closes its connection to the remote process--either the TTY or the set of pipes.

If the remote process was started with a TTY (because you specified -t), then closing the TTY will cause the remote process to receive a SIGHUP signal. SIGHUP will cause a process to terminate, unless the process specifically arranges to catch or ignore the signal.

If the remote process was started without a TTY, then closing the process's standard input/output/error has no immediate effect. The process won't receive a SIGHUP or other signal. If the process tries to read from standard input, it'll get an end-of-file condition. If it tries to write to stdout/stderr, it'll get a SIGPIPE (which terminates the process by default). If it doesn't access stdin/stdout/stderr, then none of these things will happen and the process could continue running indefinitely.

Upvotes: 3

Related Questions