How can I use ssh to run a command on a Unix machine and exit before the command completes?
For instance, if I type
ssh localhost 'sleep 60 &'
ssh hangs on my terminal until sleep completes, even though sleep is run in the background.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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
screen if you may want to come back later, to look at the progress or output.