Home » Questions » Computers [ Ask a new question ]

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

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

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

Asked by: Guest | Views: 74
Total answers/comments: 2
Guest [Entry]

"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'"
Guest [Entry]

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