17
6
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.
17
6
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.
19
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'
If I do 'ssh remotemachine -n 'long running command > withremoteredirects 2>&1 </dev/null &' and the client shuts down will the command complete? – Richard Hoskins – 2009-07-19T18:24:49.340
Afaik no. I ssh connections is lost, the ssh daemon should terminate all terminals "inside" ssh. Ans since the terminal started the command they would be terminated to, since thier parent process terminated. But it's worth trying ;) – Martin – 2009-07-19T18:32:19.477
What about 'nohup' instead of 'screen' on the server side? – Richard Hoskins – 2009-07-19T18:33:44.670
@Brad Gilbert Nothing? I can think of a few things, but one thing the don't have in common, is that 'screen' isn't available on all POSIX machines. – Richard Hoskins – 2009-07-19T18:44:24.940
http://www.gnu.org/software/screen/manual/ – Brad Gilbert – 2009-07-19T18:44:52.457
screen
and nohup
have different uses. You can't rejoin a nohup
session, only view what it has done. – Brad Gilbert – 2009-07-19T18:57:45.470
@Brad Gilbert I know. Screen isn't always available, and anyway it was being used in the example to prevent sleep from being killed when the ssh connection closed. – Richard Hoskins – 2009-07-19T19:04:42.393
6
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'
@BradGilbert That syntax is not POSIX-compatible; it's hard to argue for sacrificing compatibility in order to save a few characters. – tripleee – 2018-04-12T11:48:33.597
You could probably combine the two redirections with &>/dev/null
. – Brad Gilbert – 2009-07-19T18:13:34.547
2
Or after login in using ssh
, start screen
if you may want to come back later, to look at the progress or output.
You can also use "tmux" which I personally find easier to configure. Both applications are pretty much equivalent with each having its pros and cons. – mzuther – 2016-02-14T13:08:31.467
You probably want the
&
outside of the quotes. – Brad Gilbert – 2009-07-19T18:20:04.5301No, that just puts the whole kit and kaboodle in the background and I am unable to type my password. – Richard Hoskins – 2009-07-19T18:55:09.330