147

I'm connecting to a Linux machine through SSH, and I'm trying to run a heavy bash script that makes filesystem operations. It's expected to keep running for hours, but I cannot leave the SSH session open because of internet connections issues I have.

I doubt that running the script with the background operator, the ampersand (&), will do the trick, because I tried it and later found that process was not completed. How can I logout and keep the process running?

Greg
  • 167
doc_id
  • 1,675
  • 2
  • 14
  • 23

9 Answers9

138

The best method is to start the process in a terminal multiplexer. Alternatively you can make the process not receive the HUP signal.


A terminal multiplexer provides "virtual" terminals which run independent from the "real" terminal (actually all terminals today are "virtual" but that is another topic for another day). The virtual terminal will keep running even if your real terminal is closed with your ssh session.

All processes started from the virtual terminal will keep running with that virtual terminal. When you reconnect to the server you can reconnect to the virtual terminal and everything will be as if nothing happened, other than the time which passed.

Two popular terminal multiplexers are screen and tmux.

Screen has a steep learning curve. Here is a good tutorial with diagrams explaining the concept: http://www.ibm.com/developerworks/aix/library/au-gnu_screen/


The HUP signal (or SIGHUP) is sent by the terminal to all its child processes when the terminal is closed. The common action upon receiving SIGHUP is to terminate. Thus when your ssh session gets disconnected all your processes will terminate. To avoid this you can make your processes not receive SIGHUP.

Two easy methods to do so are nohup and disown.

For more information about how nohup and disown works read this question and answer: https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and

Note: although the processes will keep running you can no longer interact with them because they are no longer attached to any terminal. This method is mainly useful for long running batch processes which, once started, no longer need any user input.

Lesmana
  • 2,324
94

There are a few ways to do this, but the one I find most useful is to use GNU Screen.

After you ssh in, run screen. This will start another shell running within screen. Run your command, then do a Ctrl-a d.

This will "disconnect" you from the screen session. At this point, you can log out or do anything else you'd like.

When you want to re-connect to the screen session, just run screen -RD from the shell prompt (as the same use user that created the session).

EEAA
  • 110,608
78

In bash, the disown keyword is perfectly suited to this. First, run your process in the background (either use &, or ^Z then type bg):

$ wget --quiet http://server/some_big_file.zip &
[1] 1156

By typing jobs you can see that the process is still owned by the shell:

$ jobs
[1]+  Running  wget

If you were to log out at this point, the background task would also get killed. However, if you run disown, bash detaches the job and allows it to continue running:

$ disown

You can confirm this:

$ jobs
$ logout

You can even combine the & and disown on the same line, like:

$ wget --quiet http://server/some_big_file.zip & disown
$ logout

This is better than running nohup in my opinion because it doesn't leave nohup.out files littered all over your filesystem. Also, nohup must be run before you run the command — disown can be used if you only decide later on that you want to background and detach the task.

Greg
  • 167
38

The tool nohup, available on most Linux boxes will do this.

Danny Staple
  • 1,484
27

Just to be thorough, I'll point out tmux, which has the same basic idea as screen:

tmux is intended to be a modern, BSD-licensed alternative to programs such as GNU screen. Major features include:

  • A powerful, consistent, well-documented and easily scriptable command interface.
  • A window may be split horizontally and vertically into panes.
  • Panes can be freely moved and resized, or arranged into preset layouts.
  • Support for UTF-8 and 256-colour terminals.
  • Copy and paste with multiple buffers.
  • Interactive menus to select windows, sessions or clients.
  • Change the current window by searching for text in the target.
  • Terminal locking, manually or after a timeout.
  • A clean, easily extended, BSD-licensed codebase, under active development.

It is, however, approximately infinitely easier to search for on Google.

Handyman5
  • 5,397
13

Screen is overkill for just keeping processes running when you logout.

Try dtach:

dtach is a program written in C that emulates the detach feature of screen, which allows a program to be executed in an environment that is protected from the controlling terminal. For instance, the program under the control of dtach would not be affected by the terminal being disconnected for some reason.

dtach was written because screen did not adequately meet my needs; I did not need screen's extra features, such as support for multiple terminals or terminal emulation support. screen was also too big, bulky, and had source code that was difficult to understand.

screen also interfered with my use of full-screen applications such as emacs and ircII, due to its excessive interpretation of the stream between the program and the attached terminals. dtach does not have a terminal emulation layer, and passes the raw output stream of the program to the attached terminals. The only input processing that dtach does perform is scanning for the detach character (which signals dtach to detach from the program) and processing the suspend key (which tells dtach to temporarily suspend itself without affecting the running program), and both of these can both be disabled if desired.

Contrary to screen, dtach has minimal features, and is extremely tiny. This allows dtach to be more easily audited for bugs and security holes, and makes it accessible in environments where space is limited, such as on rescue disks.

sml
  • 231
9

Here's a way to daemonize any shell process, no external programs needed:

( while sleep 5; do date; done ) <&- >output.txt &

When you then close your session, the job will continue to run as evidenced by the output.txt file (which has buffering so it takes a while to show non-zero). Don't forget to kill your job after testing.

So all you need to do is close stdin and background the job. To be really good, first cd / so you don't hold on to a mount.

This works even in simple sh under Solaris.

Greg
  • 167
w00t
  • 615
7

byobu on Ubuntu is a nice front-end to screen. By pressing Ctrl-? you get a list of all the keyboard shortcuts. It adds a status bar that can be useful for watching CPU load, disk space, etc. Overall it provides an experience that I would describe as a terminal based VNC connection.

nohup allows starting a job in the background with its output routed to a log file, which can always be redirected to /dev/null if not required.

Greg
  • 167
7

The at command can be useful for this kind of situation. For example, type:

at now

And you can then enter a command or series of commands that will be run. The results should be e-mailed to you, if e-mail is set up correctly on the machine.

Instead of now, you can specify a time optionally with a date, or an time expression like now + 15 minutes. See man at for more details.

rjmunro
  • 2,361