How to suspend and bring a background process to foreground

  • 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?

    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: Even when the process outputs to stdout, I can still type `fg ` to make it foreground?

    @Tim: Yes, you can.

  • fromnaboo

    fromnaboo Correct answer

    8 years ago

    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.

    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

    The process outputs to stderr, so how shall I issue the command `job` and `fg ` while the process is outputting to the terminal?

    @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: 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?

    _Can a job running in the background be suspended directly_ yes: `kill -STOP %job_id`, as explained

    after suspend a process you can use something like `ps -eo pid,s | grep T | cut -d' ' -f1 | xargs kill -SIGCONT` to resume all process

    it would be insanely useful to me if there was a shortcut to typing `kill -STOP %1` such as `st %1` as my typing error rate seems to increase exponentially when the terminal is spewing output and I can't see what I'm typing, so the likelihood I will correctly type `kill -STOP %1` when I'm in a hurry to stop that output is a lot lower than something shorter.

    @Mihael you can write a shell script, a text file, with the top line `!/bin/bash` if that is ypur shell, the second line `kill -STOP $1` where $1 is the first parameter given to this script. Save it wirh file name 'st' in an executable folder like `/usr/local/bin/`. Then make it executable with command `chmod a+x /usr/local/bin/st` and now you can type `st %1` from everywhere.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM