How to make a terminal process run in the background

Background terminal processProbable situation: you like to have clean installations. You recover the data manually and you always install the same software after the installation of the operating system. You have a long command to install all APT packages one after another. You enter the command. You wait for it to finish. Your team is not the most powerful in the world and it suffers. Is there a solution for this? Well the fact is that we can run a terminal process in the background and in this article we will show you how.

The above situation was something that happened to me when using Ubuntu. I installed a lot of software and uninstalled another one to leave it as I liked it, but that is not necessary for me in Kubuntu because it has almost everything I need out of the box. Whatever it is, there is a way to avoid having a terminal window open which may disturb or we may shut down and stop the process by accident. This is something that cannot happen to us if we run it in the background.

bg takes a process from the terminal to the background

En this article We explain why we can not copy and paste with the shortcuts of a lifetime in the terminal. Those shortcuts are used to perform other actions, such as Ctrl + C to stop a process. In many other programs, Ctrl + Z It is used to undo the last change, but it also does not work in the terminal. What it does in the terminal is pause a process and add it to "jobs". The fastest example in which we can see how it works is to update the APT packages (sudo apt update) and press Ctrl + Z. We will see that the terminal says "[1] + Stopped", which means that we have stopped process number 1 and added it to the list of jobs for that terminal; if we go out, the jobs go with him. To restart them we will use fg to keep it in the foreground or bg so that it remains in the background. In both cases we will continue to see the process in the terminal and it will stop if we close the window.

Once a terminal process, or several, has been paused, we can see what we have pending using the command jobs that we have mentioned above. If there is more than one process stopped, we will add the number to fg o bg to resume a specific one. The option bg (background = background) does not allow us to stop the process again. If we want the process to run directly in the background, we will add "&" without the quotes.

jobs

disown allows work to continue when exiting terminal

If we want a terminal process to continue in the background after closing the window, we will use the command disown. To do this, the steps to follow would be these:

  1. We start a process.
  2. We stop it with Ctrl + Z.
  3. We wrote jobs to see the process number.
  4. We write the following command, where the number behind the percent matches the process we want to run in the background after exiting the terminal:
disown -h %1
  1. We restart the process with the following command (using 1 if that is the process we want to restart):
bg 1
  1. If we wish, we close the terminal.

The best way to check that this works is to unzip a large file. In the previous screenshot you can see how I have been decompressing a 7z file from the terminal. Depending on the size of the file, this may take several minutes. If you do what we have explained above, you can go to the path where we have told you to unzip it (by default / HOME), right-click, access its properties and check that the size is increasing little by little. If it doesn't, we close the properties window and access it again. The problem? There is no way to know precisely when a process has finished. In the case of unzipping a file, it is assumed to be complete when it is no longer increasing in size. In any case, we can verify that it continues to work after closing the terminal.

I would not like to end this article without saying something: although Everything explained here is safe, I recommend that you first check that everything works correctly in your operating system with something that is not very important. For example, downloading a 7z file and unzipping it. If you see that everything works as we have explained in this post, go ahead with everything else. Will it be useful to be able to run terminal processes in the background?


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.