HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. System Administration
  5. Managing Running Processes
  6. Backgrounding and Foregrounding

Using Foreground and Background Commands

PreviousStarting a Background ProcessNextKilling and Recining Processes

Last updated 3 years ago

Was this helpful?

We will now work through an example of managing a task with a combination of backgrounding and foregrounding. A good illustration would be the creation and editing of a text file.

To begin with, we will open the nano editor:

nano example

We will now send a signal to stop the nano process. This is known as SIGTSTP, and is issued by pressing Ctrl + z:

We can now see that the process is stopped, effectively in a frozen state, by running few commands we have previously seen, such as ps, jobs and top:

When managing processes you have stopped, jobs provides the cleanest output. ps and htop require filtering most of the time, and aren't the most elegant choice in this scenario

At the moment, we have regained control of the terminal. We can therefore work on another task, and when we are ready, we can pick back up with our stopped process. To do this, we use the fg command, followed by the job number (depicted in [ ]), to re-focus our terminal on the stopped nano process:

fg 1

What if we want to perform a task that ordinarily would tie up our terminal, but also work on something else at the same time? To achieve this, we can run a task in the background. As we saw earlier, we can start a process in the background by appending an ampersand (&) to the command - that's all well and good, but what if we didn't have the foresight to start the process this way?

We can background a running task, then set it to start back up again in the background with bg: In this example, we will run a command that ties up the terminal, and then press Ctrl + z to send SIGTSTP:

sudo tcpdump -i eth0 -ntvxw example.pcap

Now, we can see by running jobs that the task has been stopped:

Now, by entering bg 1, we can allow the process to continue in the background:

bg 1

As you can see in the output, the terminal gets a little messy. Often, when you background a process, some of its output will still get printed to the terminal. This can be modified with STDOUT redirection of course, but things can still end up a little untidy - I have left this output in this state to illustrate the point

nano open, and basic text input
The result of SIGTSTP
Showing the stopped process with ps
Showing the stopped process with htop
Showing the stopped process with jobs
The previously stopped process has been resumed
Running process is stopped with SIGTSTP
Confirmation that the running process has stopped
The process runs in the background, then it is foregrounded and terminated with Ctrl + c