15

I need to run SSH commands remotely with the output displaying locally. But if the connection breaks I want the command to still run.

I am not talking so much about logging in and executing but doing ssh user@remotehost 'commands && command etc'

How can I ensure the command runs even if the connection breaks?

vfclists
  • 1,562
  • 5
  • 20
  • 36

5 Answers5

27

The best way to do this is using screen, which keeps the session open in a persistent way even if the connection dies (and if you want to start using it again you can do a screen -r and it will open it up again).

Prefixing whatever command you want to run with screen (eg. ssh -t user@host screen command) should do the job.

If you want it to run in the background of the shell, you can also append an & to the whole thing.

Falcon Momot
  • 24,975
  • 13
  • 61
  • 92
  • You will likely need to use ssh -t to allocate a terminal for the command to run in. Otherwise the error is "Must be connected to a terminal." – bonsaiviking Apr 13 '12 at 15:13
11

Are you looking to have a persistent session that you can resume, or just to stop a disconnect from killing the exection?

For the former, use screen, for the latter, nohup.

Shane Madden
  • 112,982
  • 12
  • 174
  • 248
7

As mentioned early, screen can do all the trick. But not only what you need. I've use screen for the years and now use it every time I've login into systems. Try to install screen and create ~/.screenrc with such config:

sessionname     vcflists
startup_message off
deflogin        off
vbell           off
msgminwait      0
msgwait         10
log             off
backtick        1 0 0 hostname
caption splitonly "%?%F%{= yk}%:%{= Kk}%? %n %t%="
hardstatus alwayslastline "%{= rY}  %1`  %{= kg} %0c %{= bY}  %-w%{= Yk} %n %t %{-}%+w %=%{g}%l "

Then run screen -Rd vcf. That is the preferable invocation of screen. You will reconnect to previously created session with name vcf* (or be warned if ambiguous) or, if session not opened yet, it will be created. CTRL+A is a meta key that do everything. CTRL+A C (CTRL+A, then C) create new window (screen) inside session. CTRL+A SPACE move focus to the next window in a round.CTRL+A D detach current session keeping all windows functional between connections. If you have launch some looooong task you can detach from screen and reattach to it later to see what happens. The same happens if your ssh session is lost - screen stay alive with all opened windows and all tasks launched.

If you want to close some window inside screen - just exit from shell in it. If you want to close all shells/tasks in all windows and exit screen - just press CTRL+A CTRL+\ and press "Y" when asked.

For all the rest see man screen.

chicks
  • 3,639
  • 10
  • 26
  • 36
Kondybas
  • 6,864
  • 2
  • 19
  • 24
2

I've seen this alternative to a reliable ssh "like" connection:

http://mosh.mit.edu/

1

I would usually do:

nohup mycommand &

tail -f nohup.out
Lucas Kauffman
  • 16,818
  • 9
  • 57
  • 92
gyin
  • 199
  • 6