Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Let's say I launch a bunch of processes from a ssh session. Is it possible to terminate the ssh session while keeping those processes running on the remote machine?

share|improve this question

12 Answers 12

up vote 106 down vote accepted
+100

As screen is not maintained anymore you should look for modern alternatives like tmux.

tmux is superior for many reasons, here are just some examples:

  • Windows can be moved between session and even linked to multiple sessions
  • Windows can be split horizontally and vertically into panes
  • Support for UTF-8 and 256 colour terminals
  • Sessions can be controlled from the shell without the need to enter a session

Basic Functionality

To get the same functionality as explained in the post with the most votes you would need to do the following:

  • ssh into the remote machine
  • start tmux by typing tmux into the shell
  • start the process you want inside the started tmux session
  • leave/detach the tmux session by typing Ctrl-B and then D

You can now safely logoff from the remote machine, your process will keep running inside tmux. When you come back again and want to check the status of your process you can use tmux attach to attach to your tmux session.

If you want to have multiple session running side-by-side you should name each session using Ctrl-B and $. You can get a list of the currently running sessions using tmux list-sessions.

tmux can do much more advanced things then handle a single window in a single session. For more information have a look in man tmux or http://tmux.sourceforge.net/. A FAQ about the main differences between screen and tmux is available here.

share|improve this answer
    
Tmux is really the way to go nowadays. GNU Screen is nice, but Tmux gives you much more functionality, like attaching from multiple locations without having to detach. It's awesome. –  CraigM Nov 23 '12 at 15:24
1  
@CraigM Use screen -x -r [screenname] or screen -rx for short if you have only one screen session active. This allows you to attach an existing screen instance. –  Lekensteyn Nov 25 '12 at 10:31
    
This advice helped me with the same problem but I think it includes a typo. I am pretty sure that you need to type Ctrl-b and then d in order to leave/detach the tmux session. Certainly that is the case for the version of tmux on my Ubuntu 12.04. –  cxrodgers Dec 25 '12 at 8:31
    
thanks @cxrodgers, fixed the typo. –  fotomonster Jan 2 '13 at 10:21
    
I can't give you enough points for this, I share the unlikeness for screen and didn't knew this whole new world with tmux.Thanks! –  Hassek Jun 18 at 15:03

You could do that by using screen. Type man screen to find out more or follow this link.

Simple scenario:

  • ssh into your remote box. type screen Then start the process you want.

  • Press Ctrl-A then Ctrl-D. This will "detach" your screen session but leave your processes running. You can now log out of the remote box.

  • If you want to come back later, log on again and type screen -r This will "resume" your screen session, and you can see the output of your process.

share|improve this answer
5  
I will usually name my screen sessions using screen -S name to make it easier to connect to the correct one later. –  David Oneill Nov 24 '12 at 22:37

Screen and nohup is the better way, but if you have to detach a process already running without screen or nohup you can run disown command. With disown you can close the terminal and get the process running on the machine.

share|improve this answer
3  
This is also my favourite way to do it. I frequently use disown -a && exit –  Stefano Palazzo Oct 21 '10 at 17:45
    
Perfect. This is a lovely command and deserves all the upvotes! –  Greg Apr 29 at 10:38

You cannot do this once the process has started, you need to have set things up before you run a long running job.

You can use nohup but modern wisdom suggests you use screen or byobu as your login so you can detach and leave things running.

Screen has the advantage that you can detach from one machine and reattach from another which is handy if you want to check on long running processes that run beyond the end of the working day.

There is a reasonable getting started guide to screen here.

byobu puts an easy to use interface on top of screen with menus etc. It's also the current implementation of screen on newer ubuntu. F2 to start a new terminal F3/F4 to toggle back and forth and F6 to disconnect. Type exit to actually end terminals permanently.

share|improve this answer
    
byobu uses tmux these days.. –  scottl Nov 28 '12 at 7:04
4  
"You cannot do this once the process has started, you need to have set things up before you run a long running job." -- no, you can use disown to achieve this. See @bassgey's answer –  Rich May 23 '13 at 16:11

There are two major programs you can use to maintain programs and terminal state over multiple ssh connections. They are screen (the incumbent, but unfortunately unmaintained) and tmux (newer, actively maintained). Byobu is a front end that can run on top of their of these systems and offer additional ubuntu status information. On new installations it will use tmux as a backend, if you have an older installation of byobu and an existing config it will maintain the previous backend, be it screen or tmux.

Byobu

Byobu can be installed on the computer by doing so in a Debian-based machine:

sudo aptitude install byobu

Using yum, you do

su -c 'yum install byobu'

It's also possible to install byobu on other distributions.

Using byobu

You can start byobu by running byobu on the host machine after connecting using ssh. This will give you a shell that looks like this:

image-byobu

You can also use Byobu Terminal on a Ubuntu machine with -X option and easily have a perfectly working byobu.

Usage:

Start byobu by typing byobu.

You can press F2 to create a new window within the current session, F3-F4 to switch between the various windows.

The best part about byobu is, you dont have to actually kill the processes running in the terminal to leave the terminal. You can simply send screen/tmux (the skeleton of byobu) to background and resume the next time you come:

  • To leave byobu and keeep it running (detach) press F6.
  • The next time you come, just do byobu and you sholud be back right where you were.

    byobu-detach-attach

You can also create various byobu sessions by byobu -S session1 and so on. And you can connect to either of them when you come back.

You can do much more using Byobu. Use it! Some definitive guides are here, here or here.

share|improve this answer
    
I tried using byobu from a PuTTY-based session to my Ubuntu box, but I get the status line repeated and scrolling on the display. Although it detached properly wen pressing F6, it was not a useable solution in my setup. –  jfmessier Nov 6 '13 at 17:47
    
@jfmessier It's because PuTTY doesn't take ncurses (utf-8 basically) well. It's possible to eliminate this problem by following this thread - stackoverflow.com/questions/10731099/… –  SiddharthaRT Nov 7 '13 at 10:40

The best way is often the simplest.

nohup long-running-command &

It was made specifically for this, it even logs stdout to nohup.log

man nohup

If you want to "background" some already running tasks, then your best bet is to ctrl+z then run

bg (this will background your most recent suspended task, allowing it to continue running)

then a quick disown should keep the process running after you log out.

screen and others can do it, but that's not what there for. I recommend nohup for tasks you know your going to leave behind and bg for tasks your already running and don't want to re-start.

Keep in mind, both are bash specific. If your not using bash, then the commands could be different.

share|improve this answer

Hey, while I agreed that screen is the most efective option. You can use vncserver and then start the process on it.

Also if your only interes is to have the process running and no need to take control back of it, and utterly most important you were not aware you will need to close the session and you have the process already running, you are not of luck if you used bash as the shell

First you need to send the process to background by typing Ctrl+Z followed by bg %1 (the number depends on the job number, usually it is 1, but you can easily pull the list using the command jobs)

Finally invoke the command disown (followed by the jobid ... same as with bg command)

This will remove the parent-child relationship between your shell and the process in background, preventing it to die when your shell is terminated.

share|improve this answer
1  
This answer is the best! Why is everyone talking about screen, the question was posed post-login event, how to keep the processes running, now after logging in but before starting them. Great answer Jorge, you've really helped me! :) –  jwbensley Aug 10 '12 at 17:13
1  
Simply bg (without %1) is often enough, since the default is the current job –  Walter Tross Nov 23 '12 at 21:55

You should check out GNU Screen and see if it helps you. Depending on how you need you application to run in realtime, it may cause more issues than it solves, but at least it will allow you to resume your session as if you never left it.

How to use :

  • Use the command screen for the first start, scroll through the introduction messages, you should be handed a terminal.
  • C-a C-c opens another terminal
  • C-a C-k kills a terminal
  • You can use C-a C-Space and C-a C-Backspace to cycle through terminals
  • C-a C-a is handy if you're mostly using only two terminals
  • C-a C-d detachs the current screen session and exits screens. You can then use screen -r to resume that session. You can have several detached screen sessions at once, in this case you'll be displayed a list of available sessions.

There are many other options, for example split screens, and also all shortcuts are fully customizable.

share|improve this answer

There's a nice program called screen. It should help you.

Just do a

man screen
share|improve this answer

For a single shell script that I have running over a long period of time, I will login, and run the process in the background using '&'.

Example:

/path/to/my/script &

I've logged out and disconnected my SSH session. When I log in some time later, the script is still executing as proven by continuous data collection from the script.

share|improve this answer

Simplest answer...

ctrl+z will suspend the running program

"bg" will run it in the background

share|improve this answer
4  
Without disowning the process (with something like disown or nohup), this will not usually keep the process running after the end of the SSH session. –  Eliah Kagan Aug 13 '13 at 0:32

I was stuck in a large mv so I wasn't in a position to stop the process, setup screen and then start it again. I managed to exit the ssh session with the process running by essentially doing the following steps:

  1. ssh [server]
  2. command
  3. CTRL+Z
  4. bg
  5. disown
  6. exit

Step 3 pauses the current process (e.g. my 'mv' command). Step 4 puts the paused process in to the background and resumes it. Step 5 lets you disown the process.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.