Skip to main content
Engineering LibreTexts

08-D.9.5: Process Troubleshooting - Background / Foreground Processes

  • Page ID
    37646
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Foreground Process

    A foreground process is a process where a command or task is run directly from GUI or the command line and waits for it to finish. Some foreground processes utilize a user interface that allows for ongoing user interaction, others execute a task and simply wait while the task completes. If it is a long running task, in some instances you are not allowed to use the interface or the terminal until the task completes.

    A simple example would be the ls command from the terminal. Until the listing of files is complete you are not able to enter additional commands from that terminal.

    Background Process

    However, when a background process is launched the shell does not have to wait for the process to complete before it can run more processes. More background processes can be started, one after the other - it is only limited by the amount of memory available. To run a command as a background process, type the command and add a space and an ampersand to the end of the command.

    Managing Foreground/Background Processes

    In the following example comments have been placed between the command to help explain what is happening:

    # the yes command is executed, output is redirectd to /dev/null
    # a CONTROL-Z is entered from the keyboard - Stopping the execution of the process.
    pbmac@pbmac-server $ yes >/dev/null
    ^Z
    [2]+  Stopped                 yes > /dev/null
    # Using the jobs command we can see we have a Stopped job, 
    # the CONTROL-Z stops the job
    pbmac@pbmac-server $ jobs
    [2]+  Stopped                 yes > /dev/null
    
    # Now - start another process - using the & at the end we
    # place this command in the background at the time of execution
    # the output shows us the job number and the PID
    pbmac@pbmac-server $ yes >/dev/null &
    [3] 16229
    
    # Using the jobs command we now see we have 2 command
    # one is Stopped, one is Running in the background.
    pbmac@pbmac-server $ jobs
    [2]+  Stopped                 yes > /dev/null
    [3]-  Running                 yes > /dev/null &
    
    # Using the bg command we place job number 2 in the background
    pbmac@pbmac-server $ bg %2
    [2]+ yes > /dev/null &
    
    # Both jobs are now running in the background
    pbmac@pbmac-server $ jobs
    [2]-  Running                 yes > /dev/null &
    [3]+  Running                 yes > /dev/null &
    
    # We can kill a job using the job number
    pbmac@pbmac-server $ kill %3
    [3]+  Terminated              yes > /dev/null
    pbmac@pbmac-server $ jobs
    [2]+  Running                 yes > /dev/null &
    
    # At this point we only have a single job running
    # using the fg comand we bring it back to the foreground
    # then kill it with a CONTROL-C
    pbmac@pbmac-server $ fg %2
    yes > /dev/null
    ^C
    pbmac@pbmac-server $ 
    

    Adapted from:
    "Process Control Commands in Unix/Linux" by Shivani Ghughtyal, Geeks for Geeks is licensed under CC BY-SA 4.0


    08-D.9.5: Process Troubleshooting - Background / Foreground Processes is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?