<p>This is a follow-on question to the <a href="https://stackoverflow.com/questions/29061/how-do-you-use-ssh-in-a-shell-script">How do you use ssh in a shell script?</a> question.  If I want to execute a command on the remote machine that runs in the background on that machine, how do I get the ssh command to return?  When I try to just include the ampersand (&amp;) at the end of the command it just hangs.  The exact form of the command looks like this:</p>
<pre><code>ssh user@target &quot;cd /some/directory; program-to-execute &amp;&quot;
</code></pre>
<p>Any ideas?  One thing to note is that logins to the target machine always produce a text banner and I have <strong>SSH</strong> keys set up so no password is required.</p>

I had this problem in a program I wrote a year ago -- turns out the answer is rather complicated. You'll need to use nohup as well as output redirection, as explained in the wikipedia artcle on
nohup
, copied here for your convenience.
Nohuping backgrounded jobs is for
  example useful when logged in via SSH,
  since backgrounded jobs can cause the
  shell to hang on logout due to a race
  condition [2]. This problem can also
  be overcome by redirecting all three
  I/O streams:
```
```
nohup myprogram > foo.out 2> foo.err < /dev/null &
```
```