Linux – SSH – run command in background & disconnect

linuxssh

I need to copy a big file (about 30 Gb) in background via ssh. If I do it this way, and disconnect, it's copied well:

ssh server
# cp /file1 /file2 & >/dev/null 2>/dev/null ; disown;

But if I do this:

ssh server 'cp /file1 /file2 & >/dev/null 2>/dev/null ; disown;'

it waits until file copies. Why?

Best Answer

Try adding -f:

ssh -f server 'cp /file1 /file2 & >/dev/null 2>/dev/null ; disown;'

(Tested and works for me.)

From the man page:

-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm.