Linux process to background - relogin - how to bring process back to foreground?
Solution 1
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
- Run the program you wish to 'background'. For example, I start tailing a log file:
Press
Ctl-a, d
to detach screen.Log out, do other things, whatever you like. Here, I'll send a message to the log.
- 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).
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.
Solution 2
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)
detachThe 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.
Related videos on Youtube
Jonny
Updated on September 17, 2022Comments
-
Jonny over 1 year
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.)