-   Linux - General (
-   -   ssh-executed script does not terminate when ssh session killed (

matthewg42 03-22-2011 08:59 AM

ssh-executed script does not terminate when ssh session killed
I have a couple of programs which I execute on a remote host something like this:


ssh user@remotehost /path/to/command
It all works well, except when I kill the ssh session from the client end... the remote program continues to run until I log in and kill it.

What's weird is that Perl programs terminate ok - it's only my shell scripts which don't die. The code in the shell script is something like this:



cat  <<EOD
Some header info

while true; do
    echo "someoutput I got from somewhere"
    sleep 5

I tried explicitly trapping various signals and using them to kill the script:

... but no cigar.

I get the same problem using plink (another ssh implementation - part of the Putty suite). I tried various command line options to plink and ssh (disabling allocation of pty etc), but without success.

Anyone have any ideas?

macemoneta 03-22-2011 10:04 AM

Try the '-t option on ssh:


ssh -t user@remotehost /path/to/command

matthewg42 03-22-2011 10:50 AM

macemoneta, thanks - that works with openssh's ssh client. I just need to work out how to do the same thing with plink now... :-)

matthewg42 03-22-2011 11:01 AM

Turns out the same option with plink works too... I was sure I had tried this option, but apparently not.

So am I right in saying that because -t is used to force allocation of a pty, when the ssh session disconnects, this triggers a SIGHUP getting sent to my script, whose default action for this signal is to exit.

Thanks again macemoneta.

macemoneta 03-22-2011 03:37 PM

Without the pseudo-tty, I'm pretty sure the signal is discarded.

matthewg42 03-24-2011 08:01 AM

While the -t option solves the problem described above, it introduces a new one, and I'm struggling to understand why...

It seems that when -t is used, the remote command executes with a very naked environment, where as the exact same command executed without the -t option runs in a more fully configured environment. I assume the difference is that without -t, sshd is executing my command in a login shell, and with -t it is not.

My question is why? and can I gave both a pty allocated, and a normal environment?

macemoneta 03-24-2011 12:15 PM

Unless the shell initialization on the target system is looking for a pseudo-terminal, the environment should be the same when a command is specified. See the 'ENVIRONMENT' section of the ssh man page for setting environment variables.

All times are GMT -5. The time now is 01:36 PM.