1

I'm trying to use nohup + redirection + background operator ("&") to run a remote script via SSH and disconnect.

This works:

ssh -n fred@fredbox.local 'nohup sudo /opt/scripts/do-thing.sh arg1 arg2 >/dev/null 2>&1 '

I want to encapsulate the nohup parts inside a wrapper that runs the actual remote script, but does the nohup and output redirection. But it just doesn't work.

Example remote script "/opt/scripts/remote-exec-script.sh":

#!/usr/bin/env bash
set -o xtrace
set -o errexit
set -o nounset

ARG1=${1}
ARG2="${2}

nohup sudo /opt/scripts/do-thing.sh $ARG1 $ARG2  >/dev/null 2>&1

Putting the nohup and output redirection in the remote script itself does not work.

I do have xtrace on, so I even tried capturing stderr in the SSH line like so

ssh -n fred@fredbox.local "/opt/scripts/remote-exec-script.sh >/dev/null 2>&1"

Is this even possible?

2

I figured out the problem; it has to do with pty allocation (-tt flags on ssh) and sudo.

TL;DR: This works as I wanted it to, as long as the remote script satisfies the following conditions:

  • Does not need sudo
  • If they need sudo, the remote user has NO_PASSWORD sudo for the operations in question
  • Alternatively, one can do it without pty allocation is one uses the -S flag for sudo, to accept the password via stdin. I did not use this method.
  • All output is captured by the remote script

I used option (2) -- remote user has passwordless sudo.

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.