dr.house
dr.house

Reputation: 190

SSH command within a script terminates prematurely

From myhost.mydomain.com, I start a nc listener. Then login to another host to start a netcat push to my host:

nc -l 9999 > data.gz &
ssh repo.mydomain.com "cat /path/to/file.gz | nc myhost.mydomain.com 9999"

These two commands are part of a script. Only 32K bytes are sent to the host and the ssh command terminates, the nc listener gets an EOF and it terminates as well.

When I run the ssh command on the command line (i.e. not as part of the script) on myhost.mydomain.com the complete file is downloaded. What's going on?

Upvotes: 0

Views: 221

Answers (2)

timoiste
timoiste

Reputation: 1

I had a similar problem and in the end found out the the login-daemon's configuration was set to termninate all processes on logout.

You may system daemon login configuration for KillUserProcesses (on exit) This overrides nohup, disown a.s.o

The file is called /etc/systemd/logind.conf

# See logind.conf(5) for details.
[Login]
#NAutoVTs=6
#ReserveVT=6
KillUserProcesses=no

The KillUserProcesses - Option was set to yes in my case by default.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328760

I think there is something else that happens in your script which causes this effect. For example, if you run the second command in the background as well and terminate the script, your OS might kill the background commands during script cleanup.

Also look for set -o pipebreak which terminates all the commands in a pipeline when one of them returns with != 0.

On a second note, the approach looks overly complex to me. Try to reduce it to

ssh repo.mydomain.com "cat /path/to/file.gz" > data.gz

(ssh connects stdout of the remote with the local). It's more clear when you write it like this:

ssh > data.gz repo.mydomain.com "cat /path/to/file.gz"

That way, you can get rid of nc. As far as I know, nc is synchronous, so the second invocation (which sends the data) should only return after all the data has been sent and flushed.

Upvotes: 1

Related Questions