Share Terminal Amongst Multiple Processes

Here's how to control multiple processes in a terminal.


  • C-c - kill process

  • [mycommand] & - background new process

  • C-z - background foreground process

  • jobs - see background processes

  • fg - restore latest bg process

  • fg %1 - restore bg process by job id

  • [mycommand] &| - separate process from terminal

Processes and Terminals

You know, the accurate specifics about terminal terminology still baffle me. We have terminals, pseudo-terminals, terminal emulators, and more. For now, think of this: I open a terminal emulator program, like alacritty, and this is what I mean by "terminal" in this article.

We can run different programs as processes within a terminal, like nvim (to edit text files) or mpv (to play media).

Foreground Processes

Most commonly, when we run a program, we're are running it in the foreground. We run nvim app.clj, and we're looking at the Neovim editor, as output in the terminal. We run mpv, and an mpv video window is spawned, and we're watching some Clojure edu content. Both of these processes are in the foreground.

When a program is in the foreground, it ties up the entire terminal. Continued input affects that program (ie, typing in nvim or seek control keys in mpv).

Stop Foreground Process

To stop a process in the foreground, type C-c (control-c), the terminal will get sent the SIGINT signal, and the process will shutdown.

Background Processes

There can also be a process attached to the terminal but be in the background. A background process will not tie up your terminal input. You type still and start new processes, having multiple processes, now, in the terminal.

Start Process in Background

To start a process in the background, add a & at the end of the command, like mpv &.

The process will start, but in the background, and you can still type in your terminal.

Move a Process to the Background

What if you've already started a process, but you want to use the terminal for something else. You'll have to move the foreground process into the background. You can do this with C-z (control-z).

Let's say that you're editing some source code, but then you want to run your tests in the same terminal:

  1. nvim app.clj - foreground

  2. C-z - move to background

  3. clj -X:test - run a new foreground process in the same terminal

See Background Processes

Once I have background processes, how do I see them? I run jobs. This will give me a list something like this:

❯ jobs
[1]  - suspended  nvim temp.txt
[2]  + suspended  mpv

Restore a Background Process

To restore a process (that's in the background) to the foreground, I use fg. That will restore the latest process.

If, when running jobs, I want to restore a different process, besides, this default, I can use the job id. That is the number, in . For instance, to restore the nvim temp.txt job in the list above, one could use fg %1.

Close All Processes

To close all processees, whether in the foreground or the background, one can close the terminal.

Separate Process From Terminal

To allow the terminal to be closed without interferring with running of a process, that process must be started in such a way as to separate it from the terminal process itself. For watching mpv in its own, independent window, for instance, try: mpv &|.

I have only a crude view of what this does. I don't actually know what the name of this &| operator is. But we're marking it as background, in combo with piping it elsewhere. The elsewhere is a separate process. If anyone has a more accurate view of this operation, please let me know.

So Useful

For the terminal-dweller, these commands are so useful. You have greater control and flexibility, making space for many processes in the terminal and controlling how they coexist. Any other tips you have on the subject?