How can I use ssh to run a command on a remote Unix machine and exit before the command completes?

SSH has an -f switch which does exactly the same as using nohup on your client but makes it possible that ssh asks for a password. Use it like that:

ssh localhost -f 'command'

But: SSH will still live in background, so if you shut down the client before the command on the server finished, it will be aborted. If you want the ssh connection to be closed after executing the command, you can use screen on the server side:

ssh localhost -f 'screen -d -m sleep 60'

Use nohup, stdout/stderr redirection, and run the command in the background:

 nohup ssh localhost 'sleep 60' >/dev/null 2>/dev/null &

EDIT: Or even better, just use the ssh's -f parameter:

ssh -f localhost 'sleep 60'

Or after login in using ssh, start screen if you may want to come back later, to look at the progress or output.

Tags:

Unix

Ssh