Job Control – How to Suspend and Bring a Background Process to Foreground

background-processjob-controlshell

I have a process originally running in the foreground. I suspended by Ctrl+Z, and then resume its running in the background by bg <jobid>.

I wonder how to suspend a process running in the background?

How can I bring a background process to foreground?

Edit:

The process outputs to stderr, so how shall I issue the command fg <jobid> while the process is outputting to the terminal?

Best Answer

As Tim said, type fg to bring the last process back to foreground.

If you have more than one process running in the background, do this:

$ jobs
[1]   Stopped                 vim
[2]-  Stopped                 bash
[3]+  Stopped                 vim 23

fg %3 to bring the vim 23 process back to foreground.

To suspend the process running in the background, use:

kill -STOP %job_id

The SIGSTOP signal stops (pauses) a process in essentially the same way Ctrl+Z does.

example: kill -STOP %3.

sources: How to send signals to processes in Linux and Unix and How to manage background and foreground jobs.

Related Question