Strovent SystemsSign in
Reference

Ctrl+Z and "Stopped": get the program back with fg, bg and jobs

Ctrl+Z suspends the running program and prints [1]+ Stopped. It is still there: fg brings it back, bg lets it run in the background, jobs lists them.

You press Ctrl+Z by mistake in an editor or a long command, the screen drops back to the shell, and you see something like:

^Z
[1]+  Stopped                 vim notes.txt

The program has not ended. The shell stopped (suspended) it and gave you the prompt back. Your unsaved work is still in it.

Get it back

fg

fg puts the most recent stopped job back in the foreground, and the program redraws its screen. With several jobs, name one: fg %2.

The job control commands

| Command | What it does | |---|---| | jobs | lists the shell's jobs and their state, such as Stopped or Running | | fg %n | resumes job n in the foreground | | bg %n | resumes job n in the background, as if it had been started with & | | kill %n | sends job n a signal (terminate by default) |

In the list, + marks the current job (the one fg and bg use with no number) and - the previous one.

Ctrl+Z, Ctrl+C and Ctrl+D

  • Ctrl+Z sends the suspend character. The terminal driver stops the foreground program and the shell takes over.
  • Ctrl+C sends the interrupt character, which normally ends the program.
  • Ctrl+D at the start of a line means end of input. At an empty shell prompt it logs out: see Ctrl+D logs you out of the shell: IGNOREEOF and ignoreeof.

stty -a shows which keys the host uses: susp = ^Z, intr = ^C and eof = ^D on most Linux hosts.

"There are stopped jobs."

If you type exit (or logout, or press Ctrl+D) while a job is stopped, Bash does not leave. It prints:

There are stopped jobs.

This is a warning so you do not lose work. Run jobs, then fg each job and quit it properly. If you exit again straight away, Bash leaves and ends the stopped jobs.

Ctrl+Z in tmux, screen and SSH

  • Inside tmux or screen, Ctrl+Z goes to the program in the current pane, as above. tmux's own suspend command is on its prefix key, not on plain Ctrl+Z.
  • To suspend the ssh program on your own computer rather than the remote program, use the OpenSSH escape ~ then Ctrl+Z at the start of a line: see Kill a frozen SSH session with Enter ~. (OpenSSH escape sequences).

A job that you resume with bg is still tied to your login. To keep a command running after you disconnect, see nohup, disown or tmux: keep a command running after SSH disconnects.

Sources

Spotted a mistake? Email [email protected].