Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I sometimes have long running processes that I want to kick off before going home, so I create a SSH session to the server to start the process, but then I want to close my laptop and go home and later, after dinner, I want to check on the process that I started before leaving work. How can I do that with SSH? My understanding is that if you break your SSH connection you will also break your login session on the server, therefore killing the long running process...

share|improve this question

6 Answers 6

up vote 52 down vote accepted

Use nohup to make your process ignore the hangup signal:

$ nohup long-running-process &
$ exit
share|improve this answer
8  
This is it. No need to install screen or tmux. This is actually the good old way to do it. Sure 'screen' or 'tmux' are both wonderous apps and should be used when needed, but as simple as running a process in the background from which you can log out, go for the above. –  reiche Aug 14 '10 at 11:40
1  
is nohup like disown? –  cjac Aug 15 '10 at 9:12
11  
They have similar purposes, but they differ in many ways. nohup intercepts SIGHUP so that when the shell that ran it quits and sends SIGHUP to all its still-running children, long-running-process doesn't die. disown simply removes the specified job from Bash's child list, so it won't try to send SIGHUP at all. nohup is a program separate from the shell, so it works with all shells, whereas disown is a Bash builtin. nohup accepts the command to run, whereas disown only works after the job is started and you've backgrounded it so you can get back to the shell. –  Warren Young Aug 15 '10 at 14:00
    
Hey that's cool, I didn't know you could do that. Very useful in cases where you can't use screen for whatever reason. –  Sandy Aug 16 '10 at 13:11

If you haven't planned ahead and setup screen, etc. just do the following:

  1. If your process is running in the background: goto #3, else: Ctrl-Z to suspend foreground process. This will report the job # of the suspended process, for example:

    [1]+  Stopped                 processName
    
  2. Send processName to the background with bg %1 (using whatever the job # is following the %). This will resume processName in the background.

  3. Disown processName with disown %1 or disown PID. Use the -h flag if you want to maintain ownership until you terminate your current shell.

share|improve this answer
1  
Thanks for this! I had a job running and thought I wasn't going to be able to keep it going because I hadn't used & when I started it. This seems to work great! –  Matt Mar 25 '12 at 15:42
1  
This is amazing! I never knew Linux could let you transfer control of active processes like this. I was afraid my battery would die while SSH'ing into my server in the middle of a long running process, and this was the answer for me! –  Pluto May 20 at 21:05

I use NX NoMachine, which is free for me because it's only me. Essentially, it runs an X session on the server which you can connect to and disconnect from over and over. The X session keeps running when you're not connected. Connections can be made from anywhere. You can choose between floating windows or a single window containing a whole desktop (eg a complete Gnome desktop). The client (which you would run on your laptop) can be run on Linux, MacOS, Solaris or Microsoft Windows. In the latter case if you choose floating windows they appear individually on the Windows Taskbar.

I use my Windows XP laptop (which I need for certain Windows-specific hardware I have) as a front end for my two Linux servers using NX Nomachine. I can even print to the printer attached to my Windows laptop from Linux.

share|improve this answer

It might be worth noting that

ssh -t lala screen -rxU moo will attach to the moo session on host lala

ssh -t lala screen -S moo will create the moo session on host lala

and

ssh -t lala screen -S moo quux will create the moo session on host lala and run the program quux, quitting the session on completion.

share|improve this answer

What you want to use is screen or even better a user-friendly wrapper around screen called byobu.

Screen allows you to run multiple virtual terminal sessions in the same ssh session. A tutorial and help pages are available.

byobu is a wrapper that allows to easily open new screens with a simple function key instead of key combination from ctrl-a. It also shows a status line with all the open virtual terminals which can be named.

Another nice feature is the fact that all your screen can stay up while your ssh connection is disconnected. You just connect again via ssh and call byobu and everything is like before.

At last some screenshots of byobu.

share|improve this answer

You want to be using GNU Screen. It is super awesome!

ssh me@myserver.com
screen               #start a screen session
run-a-long-process

CTRL+a , d to detatch from your screen session

exit                 #disconnect from the server, while run-a-long-process continues

When you come back to your laptop:

ssh me@myserver.com
screen -r            #resume the screen session

Then check out the progress of your long-running process!

screen is a very comprehensive tool, and can do a lot more than what I've described. While in a screen session, try ctrl+a,? to learn a few common commands. Probably the most common are:

  • CTRL+a , c to create a new window
  • CTRL+a , n to switch to the next window in your screen session
  • CTRL+a , p to switch to the previous window in your screen session
  • if you log in from a bunch of different systems, you may have accidentally left yourself attached to an active screen session on a different computer. for that reason, I always resume with screen -d -r to ensure that if another shell is attached to my screen session, it will be detached before I resume it on my current system.
share|improve this answer
4  
tmux is a modern analogue of screen. –  zvolkov Aug 14 '10 at 2:21
    
I don't know about modern, but default key prefix of tmux (Ctrl + b) helps if you're used to bash/emacs keys. –  sajith Aug 14 '10 at 4:15
1  
screen is pretty cool if you want a shared terminal for some reason. Create a screen screen -S name and let your other friend connect to it with screen -x name. –  Patrick Oct 6 '10 at 9:23
    
Excellent answer –  Muneer Jun 14 '11 at 7:57
    
tmux with tmuxinator is a great combination for fancy setups, while I prefer screen as a quick and simple solution. –  earthmeLon Jun 19 at 15:31

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.