I'd like to start a long running script via SSH and immediately return. Afterwards, I want to check the remote host every 5 minutes to see if the job is done.

In order to accomplish the first part, I used this trick:

ssh root@remoteserver 'TMP_FILE=$(mktemp --tmpdir backup.XXXXXXXXXX.log) && (/root/backup.sh </dev/null > $TMP_FILE 2>&1 &) && echo $TMP_FILE'

Now, for the second part, since backup.sh can be any script, I'd like to append something like DONE_$TMP_FILE to the file pointed to by $TMP_FILE when backup.sh terminates and, every 5 minutes, run this command:

ssh root@remoteserver 'tail -1 $TMP_FILE'

to see if the last line is equal to DONE_$TMP_FILE.

The problem is that I can't manage to detach from a composite command:

ssh root@remoteserver 'TMP_FILE=$(mktemp --tmpdir backup.XXXXXXXXXX.log) && (/root/backup.sh </dev/null > $TMP_FILE 2>&1 && echo DONE_$TMP_FILE >> $TMP_FILE &) && echo $TMP_FILE'

The above command will simply block until backup.sh finishes running.

Is there any way to detach from a sequence of commands without placing them in a wrapper shell script?

If there is any better way of doing this, I'd love to hear it. Ideally, I shouldn't need to create any auxiliary shell scripts.


It turns out the solution is easier than I thought:

ssh root@remoteserver 'TMP_FILE=$(mktemp --tmpdir backup.XXXXXXXXXX.log) && ((/root/backup.sh && echo DONE_$TMP_FILE) </dev/null > $TMP_FILE 2>&1 &) && echo $TMP_FILE'
| improve this answer | |

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.