bgResume a stopped job in the background
bg resumes a job you stopped with Ctrl-Z, but in the background, so the prompt stays yours. It's the fix for 'I started a long command and forgot the &': Ctrl-Z, then bg.
bg [%JOB...]You started a big archive in the foreground and need your prompt back without starting over.
bgbash builtin: sends SIGCONT to a stopped job without giving it the terminal.
bg looks up the job in the shell's job table, marks it as running in the background and sends SIGCONT to its whole process group with killpg. Unlike fg, it does not call tcsetpgrp, so the terminal stays with the shell and keyboard signals (Ctrl-C, Ctrl-Z) no longer reach the job. The job's stdin, stdout and stderr still point at the terminal: output is printed in between your commands (unless stty tostop is set, which stops writers with SIGTTOU), and if the job tries to read from the terminal the kernel sends it SIGTTIN, which stops it again. bg is the same as having started the command with & in the first place, except that you decided after it was already running.
(no argument)Resume the current job (+) in the background%NResume job number N%N %MResume several jobs at once%string / %?stringJob whose command starts with / contains stringCtrl-Z, then logging out, assuming the job keeps runningCtrl-Z stops the job; it does nothing until resumed. On logout it gets SIGHUP (and dies) or stays stopped forever.
Ctrl-Z, bg, then disown -h %1 (or use nohup/tmux from the start)bg on a command that prompts for inputA background job that reads the terminal is stopped by SIGTTIN, so it never gets anywhere.
Answer the prompt with fg, or run it non-interactively (--yes, < /dev/null)Putting a chatty command in the background with bgIts output still goes to the terminal and gets mixed with whatever you type next.
Start it with output redirected: cmd > out.log 2>&1 &Try these on a Linux machine of your own. Each task has a hint and a solution.
Run `sleep 300` in the foreground, stop it, and resume it in the background. Check that it's running.
Stop two foreground sleeps one after the other, then resume both with one bg command.
Background a stopped job, then detach it from the shell so it survives logout.
bgResume a stopped job in the background
bg resumes a job you stopped with Ctrl-Z, but in the background, so the prompt stays yours. It's the fix for 'I started a long command and forgot the &': Ctrl-Z, then bg.
bg [%JOB...]You started a big archive in the foreground and need your prompt back without starting over.
bgbash builtin: sends SIGCONT to a stopped job without giving it the terminal.
bg looks up the job in the shell's job table, marks it as running in the background and sends SIGCONT to its whole process group with killpg. Unlike fg, it does not call tcsetpgrp, so the terminal stays with the shell and keyboard signals (Ctrl-C, Ctrl-Z) no longer reach the job. The job's stdin, stdout and stderr still point at the terminal: output is printed in between your commands (unless stty tostop is set, which stops writers with SIGTTOU), and if the job tries to read from the terminal the kernel sends it SIGTTIN, which stops it again. bg is the same as having started the command with & in the first place, except that you decided after it was already running.
(no argument)Resume the current job (+) in the background%NResume job number N%N %MResume several jobs at once%string / %?stringJob whose command starts with / contains stringCtrl-Z, then logging out, assuming the job keeps runningCtrl-Z stops the job; it does nothing until resumed. On logout it gets SIGHUP (and dies) or stays stopped forever.
Ctrl-Z, bg, then disown -h %1 (or use nohup/tmux from the start)bg on a command that prompts for inputA background job that reads the terminal is stopped by SIGTTIN, so it never gets anywhere.
Answer the prompt with fg, or run it non-interactively (--yes, < /dev/null)Putting a chatty command in the background with bgIts output still goes to the terminal and gets mixed with whatever you type next.
Start it with output redirected: cmd > out.log 2>&1 &Try these on a Linux machine of your own. Each task has a hint and a solution.
Run `sleep 300` in the foreground, stop it, and resume it in the background. Check that it's running.
Stop two foreground sleeps one after the other, then resume both with one bg command.
Background a stopped job, then detach it from the shell so it survives logout.