Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
You can still type commands in a terminal that is spewing errors. The text spewed on STDERR doesn't count as input, only the keys you send. It looks confusing on screen but it works. –  Caleb Aug 8 '12 at 19:42
    
@Caleb: Even when the process outputs to stdout, I can still type fg <jobid> to make it foreground? –  Tim Aug 8 '12 at 19:59
    
@Tim: Yes, you can. –  Caleb Aug 8 '12 at 21:22
add comment

2 Answers

up vote 24 down vote accepted

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 send the vim 23 process back to foreground.

To suspend the process running in the background, use:

kill -19 %job_id.

the -19 signal is SIGSTOP (the signal sent by Ctrl - Z) .

example: kill -19 %3.

sources: one and two

share|improve this answer
2  
signal 19 is SIGCONT for me; I use kill -STOP and kill -CONT in preference to remembering the numbers anyway, but you can check kill -l to remind yourself of the numeric values –  Useless Aug 8 '12 at 13:04
    
The process outputs to stderr, so how shall I issue the command job and fg <jobid> while the process is outputting to the terminal? –  Tim Aug 8 '12 at 13:19
1  
@Tim Just type the command as you normally would. As long as the job is backgrounded, it is not reading what you type - your shell is. What you type might look broken up to you, but the shell will understand it just fine. –  AlexWebr Aug 8 '12 at 14:30
    
@AlexWebr: Thanks, it works! (1) Does a background job not accept any input and output including "Ctrl+Z" etc, right? (2) Can a job running in the background be suspended directly? If yes, how? If no, must it be first changed to run in the foreground before it can be suspended? –  Tim Aug 8 '12 at 15:02
2  
Can a job running in the background be suspended directly yes: kill -STOP %job_id, as explained –  Useless Aug 8 '12 at 16:38
add comment

Type fg to bring it to the foreground.

share|improve this answer
    
Thanks! The process outputs to stderr, so how shall I issue the command fg <jobid> while the process is outputting to the terminal? –  Tim Aug 8 '12 at 13:16
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.