LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-22-2011, 08:59 AM   #1
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.

Anyone have any ideas?
 
Old 03-22-2011, 10:04 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Try the '-t option on ssh:

Code:
ssh -t user@remotehost /path/to/command
 
1 members found this post helpful.
Old 03-22-2011, 10:50 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Original Poster
Rep: Reputation: 65
macemoneta, thanks - that works with openssh's ssh client. I just need to work out how to do the same thing with plink now... :-)
 
Old 03-22-2011, 11:01 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Original Poster
Rep: Reputation: 65
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.
 
Old 03-22-2011, 03:37 PM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Without the pseudo-tty, I'm pretty sure the signal is discarded.
 
Old 03-24-2011, 08:01 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Original Poster
Rep: Reputation: 65
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?
 
Old 03-24-2011, 12:15 PM   #7
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
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.
 
  


Reply

Tags
kill, perl, script, shell, ssh



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] If I exit an SSH session, would it terminate transfers from one server to another? imekul Linux - Server 2 09-09-2010 02:17 AM
How to terminate Bash Script if remote session disconnects zia.hassan Linux - Newbie 4 06-15-2009 04:04 AM
ssh session within a bash script Tamaros Programming 1 05-11-2008 03:38 AM
Run intractive ssh session in a bash script MasterC Programming 2 10-15-2007 05:08 PM
ssh -> perl -> spawn background proces hangs ssh session rhoekstra Programming 2 04-25-2006 01:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration