How to kill a background ssh process? - SSH
This is a discussion on How to kill a background ssh process? - SSH ; I need to have a background ssh tunnel.
It is setup like this:
$ ssh -n -N -C -o "StrictHostKeyChecking no" -L 5433:localhost:5432
1.2.3.4 &
It needs to eventually be killed and restarted with another server IP.
What I'm doing ...
-
How to kill a background ssh process?
I need to have a background ssh tunnel.
It is setup like this:
$ ssh -n -N -C -o "StrictHostKeyChecking no" -L 5433:localhost:5432
1.2.3.4 &
It needs to eventually be killed and restarted with another server IP.
What I'm doing is sending it a SIGTERM and it works but leaves a
defunct process. This is not too big a deal but I want it clean.
I have read from the man that a ~ followed by a . from stdin closes the
connection, but the manual also states that the -n option must be used
when ssh is to run in the background.
What is the best approach?
Regards, Clodoaldo PInto
-
Re: How to kill a background ssh process?
>>>>> "CP" == clodoaldo pinto writes:
CP> I need to have a background ssh tunnel. It is setup like this: $
CP> ssh -n -N -C -o "StrictHostKeyChecking no" -L 5433:localhost:5432
CP> 1.2.3.4 &
CP> It needs to eventually be killed and restarted with another server
CP> IP. What I'm doing is sending it a SIGTERM and it works but
CP> leaves a defunct process. This is not too big a deal but I want it
CP> clean.
CP> I have read from the man that a ~ followed by a . from stdin
CP> closes the connection, but the manual also states that the -n
CP> option must be used when ssh is to run in the background.
CP> What is the best approach?
Try using -f instead of having the shell put it under job control with "&".
CP> Regards, Clodoaldo PInto
--
Richard Silverman
res@qoxp.net
-
Re: How to kill a background ssh process?
On 2006-08-18, clodoaldo.pinto@gmail.com wrote:
> I need to have a background ssh tunnel.
>
> It is setup like this:
> $ ssh -n -N -C -o "StrictHostKeyChecking no" -L 5433:localhost:5432
> 1.2.3.4 &
>
> It needs to eventually be killed and restarted with another server IP.
> What I'm doing is sending it a SIGTERM and it works but leaves a
> defunct process. This is not too big a deal but I want it clean.
So whatever's spawning ssh isn't catching the SIGCHLD when it exits and
wait()ing for the exit status?
> I have read from the man that a ~ followed by a . from stdin closes the
> connection, but the manual also states that the -n option must be used
> when ssh is to run in the background.
>
> What is the best approach?
If you're using a relatively recent OpenSSH then you can also use the
ControlPath socket to send an "exit command", eg:
$ ssh -MS /tmp/sock -L [forwardspec] yourserver
then you can use this to close the connection:
$ ssh -S /tmp/sock -O exit yourserver
The "-O exit" option first appeared in OpenSSH 4.2. You can put the
controlpath bits into ~/.ssh/config or ssh_config.
As Richard said, you probably want to use "-f" too.
--
Darren Tucker (dtucker at zip.com.au)
GPG key 8FF4FA69 / D9A3 86E9 7EEE AF4B B2D4 37C9 C982 80C7 8FF4 FA69
Good judgement comes with experience. Unfortunately, the experience
usually comes from bad judgement.
-
Re: How to kill a background ssh process?
Darren Tucker wrote:
> On 2006-08-18, clodoaldo.pinto@gmail.com wrote:
> > I need to have a background ssh tunnel.
> >
> > It is setup like this:
> > $ ssh -n -N -C -o "StrictHostKeyChecking no" -L 5433:localhost:5432
> > 1.2.3.4 &
> >
> > It needs to eventually be killed and restarted with another server IP.
> > What I'm doing is sending it a SIGTERM and it works but leaves a
> > defunct process. This is not too big a deal but I want it clean.
>
> So whatever's spawning ssh isn't catching the SIGCHLD when it exits and
> wait()ing for the exit status?
Exactly. I just wait()ed for the exit status and all is good now.
Thanks, Clodoaldo PInto