ssh process control (terminal magic?)



Hi, on Linux (Ubuntu 8.04), when I start ssh as a background shell job (I have passwordless login set up), by hitting enter a couple of times, after about 3 seconds since the original command is issued I see that the ssh process is suspended:

$ ssh hv sleep 30 &
[1] 22468
$ <hit enter quickly>
$
$

[1]+ Stopped ssh hv sleep 30
$

Now if I have something that's continuously producing stdout, then I see the output until after about 3 seconds in:

$ ssh hv 'while true; do echo a; sleep 1; done' &
[1] 22486
$ a
<hit enter>
$ a
a
a
a
a
a
<after waiting, hit enter>
[1]+ Stopped ssh hv 'while true; do echo a; sleep 1; done'
$ <no more output>

Attempts to bg it don't work:

$ bg
[1]+ ssh hv 'while true; do echo a; sleep 1; done' &
$

[1]+ Stopped ssh hv 'while true; do echo a; sleep 1; done'
$

And fg can bring it back, but only after hitting enter:

$ ssh hv 'while true; do echo a; sleep 1; done' &
[1] 22596
$ a
a
a


[1]+ Stopped ssh hv 'while true; do echo a; sleep 1; done'
$ fg
ssh hv 'while true; do echo a; sleep 1; done'
<nothing is printed...until I hit enter, at which all the buffered-up "a"s are immediately dumped>
a
a
a
a
a
a
a


Furthermore, I can't kill this process using sigint, sigterm, sigquit, etc.:

$ ps
PID TTY TIME CMD
22515 pts/3 00:00:00 ssh
22546 pts/3 00:00:00 ps
32443 pts/3 00:00:03 bash
$ kill 22515
$
$

But I can kill it with the shell's job control kill:

$ kill %1
$
[1]+ Exit 143 ssh hv 'while true; do echo a; sleep 1; done'
$

Alternatively, I could fg the job, at which point it will immediately exit (handle the queued-up signals). Could anyone help explain this behavior? I'm interested in writing scripts to run commands remotely via ssh, but I clearly don't get how to control ssh. If it matters, this is all in "OpenSSH_4.7p1 Debian-8ubuntu1, OpenSSL 0.9.8g 19 Oct 2007". Thanks in advance for any guidance!