ssh-executed script does not terminate when ssh session killed
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
Code:
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:
Code:
#!/bin/bash
cat <<EOD
Some header info
EOD
while true; do
echo "someoutput I got from somewhere"
sleep 5
done
I tried explicitly trapping various signals and using them to kill the script:
Code:
trap exit SIGHUP SIGQUIT SIGTERM
... 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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.