Sunday, June 3, 2007

I always forget this, how to place a process in the background after it is already running

So you started this process in your terminal and it is taking a really long time to complete. You want that terminal back but it seems too late to kill the process and rerun it with the '&' to detach it into the background. Well there is hope. You can complete the following to place the process in the background and get your terminal back.

1. From the terminal in question, the one where you do not have a prompt because the application is still running, hit Ctrl-z (this signals the process to pause, but not stop, it can be picked up again right where it left off)
2. Then type 'jobs', if you only have one job on that terminal (you most likely do), then this stopped job will be '1'.
3. Then type 'bg 1' or whatever the jobs output was. Blamo, you have restarted your process into the background.
4. If you logout of the terminal (pty) which launched the process, you are essentially orphaning the process. This will cause the process to zombie (which means the process will never end/die because it is waiting on a signal from the parent that it received its result code) when the process finally does end. Also, if the process outputs to stdout/stderr, then there will be no pty parent associated with it, so Linux will cleanup this process.
5. Thanks to Charles Jones for pointing out that you can issue a 'disown %1' where 1 is the job number.

No comments: