In this tutorial you are going to learn how to run a process in the background. You will also learn how to move a background process into the foreground, and vice versa.
When we execute a command in Linux from a shell the process it creates run in the foreground, attaching itself to our session. This means mean we will not be able to interact with our shell until the process exits.
While this may be desirable for short-lived processes, such as running a
find on your filesystem. It isn’t always desirable for longer lived processes, such as running a development web server.
Placing a Running Foreground Process into the Background
A foreground process is the a process. In order to place a foreground proces into the background, we must first put the process to sleep, and then place it in the background.
- Execute the command to run your process.
CTRL+Zto put the process into sleep.
- Run the
bgcommand to wake the process and run it in the backround.
Placing a Running Background Process into the Foreground
A background process can be return to the foreground if needed. We use the
fg command to achieve this.
fg command on its own, without a process ID, will force your last execute process from your session into the foreground.
To foreground a specific process you should include the process ID.
Starting a Process in the Background
Finally, a process created by executing a command from the command-line can be forced to start in the background. To so we append an ampersign (&) at the end of our command. This instructs the shell, such as Bash, to start the process in the background.
find / -type f -name="*.txt" &
You will be given the Job ID of the process and return to an interactive shell.