Search code examples
bashsshpipebackground-processnetcat

Dropping <some command> | netcat to the background via ssh


For a project, I am using a remote machine to run a binary and pipe the output to a log server via netcat.

Example:

ssh <user>@<host> -p <port> -o IdentitiesOnly=yes -o PreferredAuthentications=publickey -i key nohup <some long running command> | nc <log_host> <log_port> &

What I want is for the ssh connection to exit as soon as the commands go into the background.

The process successfully drops to the background (I believe), but I am unable to figure out how to terminate the ssh connection.

I have tried to use screen too, but that was after messing with this way a lot and frankly I was very tired and getting really confused, and had trouble figuring out the various different problems with it. I believe a second set of eyes is pretty crucial to make progress at this point.

Just for note, I have tried a simpler test command and that doesn't work either. ssh waits for the entire three seconds, rather than forking off the background command and immediately terminating.

ssh <user>@<host> "nohup sleep 3 && echo test | tee ~/test.log & exit"

Solution

  • I think this is a problem with ssh waiting for I/O from standard output and standard error.

    Try redirecting all output to /dev/null:

    ssh user@host "(nohup sleep 3 && echo test | tee ~/test.log) >/dev/null 2>&1 &"