I am having this interesting problem, where I would like to start this command

nc -l 8023 | zfs receive tank3/pro1

on a remote host

If I run

ssh -n 'nohup nc -l 8023 | zfs receive tank3/pro1 &'

from inside a script, then it doesn't continue in the script, because zfs it is waiting for STDIN.


ssh -n 'nohup nc -l 8023 | zfs receive tank3/pro1 &> /dev/null < /dev/null &'

then the script continues, but redirecting STDIN destroys the zfs command.


Can screen be used to solve this?

Or other ideas?

migrated from stackoverflow.com Feb 22 '13 at 6:09

This question came from our site for professional and enthusiast programmers.


You can try following example:

ssh -t root@server1 screen "tail -f /var/log/messages"

-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful

  • It starts tail on the remote host in screen, but doesn't give me the shell back on the local host, so my script can continue. – Sandra Feb 22 '13 at 16:53
  • That's because tail -f doesn't exit; it follows the file forever, until you explicitly kill it. – chepner Feb 23 '13 at 0:38

I dont have ZFS, but I made a simple script to mimic reading stdin. Seems to work for me


while read X
   echo $X

And then:

ssh localhost "nc -l 8023 | ~/sand/readme.sh &\\disown"

Or with single quotes:

ssh localhost 'nc -l 8023 | ~/sand/readme.sh &\disown'

\ separates multiple remote ssh commands, or at least seems to behave this way for me :). disown will allow you to break from a backgrounded job. I use this whenever I run a long command and realize only after I should have done it in a screen. CTRL+Z; bg; disown if you ever find yourselves in that scenario. You can logout of SSH and it will continue to run in background.

  • Very interesting command! It works with your example, but not with zfs for some reason. – Sandra Feb 22 '13 at 16:54

You can indeed use screen for this, with -d -m to start in detached mode:

ssh screen -d -m 'nc -l 8023 | zfs receive tank3/pro1'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.