I want to remotely execute a script that begins synchronously (so that the local ssh command fails if the preparatory commands fail) and then goes asynchronous, thus terminating the local ssh command.
I have a script on a remote server that slowly downloads a multi-gigabyte file. I would like to launch this script via SSH from the local server with ssh remote-server 'nohup /path/to/script arguments' but kill the SSH connection when I know that the script has been successfully started the download. Once it is launched, the SSH connection doesn't serve any useful purpose, systematically fails somewhere during the downloads, and blocks the execution on the local server.
I can't just do
ssh -f or
ssh & because I need the command to fail on the local server server if the remote script doesn't launch, fails on the first commands before the download, or if the remote server is not reachable.
I have tried various
screen tricks. The closest I got was the following:
Launched by the local server:
ssh -t me@remote-server 'screen -S long-download /path/to/download.sh'
ssh -t me@remote-server 'nohup screen -S long-download /path/to/download.sh'
download.shlaunched on the remote server:
preliminary-instructions # if anything fails so far then the local server's ssh command should fail synchronously download-command & some-more-checking screen -d long-download # we can now safely end the ssh session
screen still gets killed…
Sample reproduction code
On a remote server, in
#!/bin/bash # uncomment this line to validate synchronous failure sleep 10 && echo foo >/tmp/bar & screen -d long-download
ssh -t me@remote-server 'nohup screen -S long-download /path/to/test.sh'