TL;DR
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.
Details
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 nohup
and 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'
or
ssh -t me@remote-server 'nohup screen -S long-download /path/to/download.sh'
In
download.sh
launched 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
But somehow screen
still gets killed…
Sample reproduction code
On a remote server, in
/path/to/test.sh
:#!/bin/bash # uncomment this line to validate synchronous failure sleep 10 && echo foo >/tmp/bar & screen -d long-download
Locally:
ssh -t me@remote-server 'nohup screen -S long-download /path/to/test.sh'