The bg and fg commands in Linux: managing background processes

Introduction

In Unix-like systems, each command executed from the terminal creates a process. By default, those processes run in the foreground, meaning the shell waits for them to finish before accepting new commands. However, there are situations where it is useful to let a process continue running while we keep working in the terminal. For this, Linux provides the bg and fg commands, which allow moving processes between foreground and background.

What are foreground and background processes

A foreground process blocks the terminal: you cannot type any other command until it finishes or is suspended. A background process, on the other hand, continues running but the shell becomes available immediately. This is achieved by suspending (with Ctrl+Z) and then resuming in the background with bg, or by directly launching the process with the & operator at the end of the line.

How to send a process to the background with & and bg

The simplest way to start a process in the background is to add the & symbol at the end of the command. For example:

$ find / -name "*.log" 2>/dev/null &
[1] 12345

The shell shows the job number (in brackets) and the PID. The process continues running while the terminal is free for other commands.

If you already have a running process and you want to send it to the background without terminating it, first suspend it with Ctrl+Z. The shell will show something like:

$ find / -name "*.tmp" 2>/dev/null
^Z
[1]+  Stopped                 find / -name "*.tmp" 2>/dev/null

Then, with the bg command you tell it to continue execution in the background:

$ bg
[1]+ find / -name "*.tmp" 2>/dev/null &

From that moment on the process keeps running and the terminal is available.

Bring a process to the foreground with fg

When you need to interact again with a process that is in the background (for example, to provide input or view its output in real time), you bring it to the foreground using fg. If there is only one background job, simply type:

$ fg

If there are several jobs, you can specify the job number:

$ fg %2

The process will again block the terminal until it finishes or you suspend it again with Ctrl+Z.

Practical examples

  • File compression in the background: $ tar -czf copia.tar.gz /home/usuario/documentos &
  • Running a long script and monitoring its output:
    $ ./script_largo.sh
    ^Z
    [1]+ Stopped                 ./script_largo.sh
    $ bg
    [1]+ ./script_largo.sh &
    $ jobs
    [1]+  Running                 ./script_largo.sh &
    $ fg
    
  • Switching between multiple jobs:
    $ ping -c 100 8.8.8.8 &
    [1] 6789
    $ sleep 60 &
    [2] 6790
    $ jobs
    [1]-  Running                 ping -c 100 8.8.8.8 &
    [2]+  Running                 sleep 60 &
    $ fg %1   # bring the ping to the foreground
    ^Z
    [1]+  Stopped                 ping -c 100 8.8.8.8
    $ bg %1   # put it back in the background
    [1]+ ping -c 100 8.8.8.8 &
    

Considerations and best practices

  • Background processes continue to consume CPU and memory resources; monitor their use on systems with limitations.
  • Jobs that attempt to read from standard input (stdin) will automatically stop when they try to read and no data is available; in those cases you need to bring them to the foreground to provide input.
  • Use the jobs command to list current jobs and their states.
  • If you close the terminal while there are background jobs, they will receive the SIGHUP signal and terminate, unless you have disowned them with disown or started them with nohup.
  • For processes that must continue running after closing the session, combine nohup with & or use disown after putting them in the background.

Conclusion

Mastering the bg and fg commands allows you to efficiently manage the execution of long-running tasks or those that do not require constant interaction, keeping the terminal free for other operations. Learning to suspend, resume, and switch between foreground and background is a fundamental skill for any Linux user or administrator who wants to work productively from the command line.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .