Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I start a process, put it in the background running, then log out, then log in again, and want to bring the process to front again. I've looked in "ps aux" for the PID, but it won't be put in the foreground with "fg ###". What's the correct way?

(I use the same account for both login sessions.)

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

The most common way to accomplish this is probably with GNU Screen.

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

Basically you will:

  • Make sure screen is installed. Typically this is the package 'screen' on most Linux distributions. It is installed by default on Mac OS X.
  • Execute screen

alt text

alt text

  • Run the program you wish to 'background'. For example, I start tailing a log file:

alt text

  • Press Ctl-a, d to detach screen.

  • Log out, do other things, whatever you like. Here, I'll send a message to the log.

alt text

  • When you log in again, type screen -r to reattach the session you were running before. Here you can see the message I sent to the log (and an earlier test, too).

alt text

Screen is considered by many to be a power user tool on Linux/Unix, and I'm inclined to agree!

For more information about screen, see its home page, or this in depth article from Ars Technica.

share|improve this answer
add comment

AFAIK there's no direct way to foreground a process that was started and backgrounded in another shell. There are a couple of ways to get around this, however.

The best way is to use GNU screen. Start a screen session, start your process, detach from the screen session, log out, log back in, reattach to the screen session. The process is still running and screen keeps any output in the window buffers. You don't even need to background it; you can leave it running in one screen window and use others for other tasks.

The ugly hackish way is to detach it from the terminal before logging out. In bash, disown -h %[jobid] does this; other shells like tcsh do this automatically for background processes when you exit the shell. (Get the $[jobid] by running the command jobs.) You can't reattach to the process directly, but if all you need is a process's stdout/stderr/stdin, you can use GDB to fake the reattaching. This is a partial how-to from this source (originally included in another answer):

[...] with some dirty hacks, it is not impossible to reopen a process' stdout/stderr/stdin.

So you could still create a blank screen window (for instance that runs sleep).

And then use gdb for instance to attach to the process, do some call close(0)
call close(1)
call close(2)
call open("/dev/pts/xx", ...)
call dup(0)
call dup(0)
detach

The process' output would go to screen. It wouldn't be attached to that screen terminal, so for instance[sic] would kill the "sleep" command, not the process, but that could be enough for the OP.

There are some ways of making bash do automatic disowns, but they involve shopts or nohup or other bash tricks to avoid the automatic SIGHUP. bash isn't as graceful as tcsh in this area, and you have to know ahead of time that you'll need this to setup the option. It's a little easier to remember to run disown on your background job before exiting.

share|improve this answer
add comment

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.