Prerequisites:
To perform the steps in this guide, you need the following components:
- A functional Linux system. Learn more about setting up an Ubuntu VM on VirtualBox.
- Access to a non-root user with sudo permission.
Running the Processes from the Terminal
Whenever running a command, the processes are spawned under the terminal. If the terminal is closed, all the associated processes are automatically terminated. In certain situations, it can be inconvenient. For example:
- Overwhelming amount of output data and error/diagnostic messages
- Accidental closure of the terminal which results in the termination of potentially mission-critical processes, etc.
To tackle these issues, there are a couple of options:
- Running the processes in the background
- Detaching the processes from the terminal
The processes that run in the background won’t overwhelm the terminal output. Moreover, the terminal is free to run the additional commands. For detached processes, they won’t terminate even if the terminal is closed.
Starting the Processes in the Background
In many cases, a command can take quite a while to complete. Generally, the user is forced to wait until the command finishes. For example, when you’re using a text editor, the shell is unavailable until the editor is closed.
To demonstrate, we use the “yes” command:
Method 1:
There are a couple of ways where we can send the running process to the background.
First, we stop the process without terminating it. To do so, run the following command and press “Ctrl + Z”:
Now, run the “bg” command to send the process in the background:
Once pushed into the background, the process resumes. The following command lists all the running jobs in the background:
Method 2:
In the previous method, the process first started running in the foreground. We paused the process, sent it to the background, then resumed it. Instead, we can run the process directly in the background.
To do so, add the “&” sign at the end of the command:
Here, the spawned processes automatically run in the background. To verify, check the list of background jobs:
Method 3:
We can also launch the processes in the background with the help of tmux, a powerful multiplexer that can manage multiple terminal sessions within a single window. It doesn’t come pre-installed in Linux. However, it’s available for all the major Linux distros. Learn more about tmux installation on Linux.
In the following example, we use tmux to ping a target and log the output:
The log file verifies whether the command runs successfully or not:
It’s just one example of what tmux can do. Learn more about tmux with examples.
Returning the Jobs to the Foreground
If you desire to return a job to the foreground, we use the “fg” command. We also need the job number from the jobs command.
First, determine the job that you want to bring to the foreground:
Now, use the “fg” command:
Detaching the Processes from the Terminal
Any process associated with a terminal is terminated once the terminal closes, whether it’s running in the foreground or in the background. To avoid the process termination, we dissociate the target process from the terminal/shell.
Method 1:
To disown a process, we first need a background process:
Check the list of running background jobs:
Note the serial number of the target background job. Now, use the “disown” command to detach it from the terminal:
The target job should now disappear from the jobs list:
However, you can still see the process running in the background:
Method 2:
Another way of detaching a process from the parent terminal is using the “nohup” command. It keeps a process running in the background even after closing the terminal.
The usage of “nohup” is as follows:
Verify if the job is created successfully:
Method 3:
This method completely detaches the target process. It’s a more effective way of detaching the GUI apps.
For example, to start the Firefox and detach it completely from the terminal, use the following command:
Here:
- The /dev/null is a special device in Linux that gets rid of any data that is written to it.
- In the previous command, the input is read from and the output is sent to /dev/null. Learn more about the other ways of using the /dev/null.
Conclusion
We demonstrated the various ways of running a process in the background. We also showcased the ways to detach a process from the parent terminal. These techniques can be useful whenever working with the terminal or running a script.
If you need to run certain commands in a specific pattern, we can offload it as a systemd service. For remote machines, we can also use the third-party tools like Ansible to automate almost everything.
Happy computing!