One of the most frustrating experience when working on the shell or terminal is the limited control you have when a task is undergoing processes or running, for example, if you try installing a package, say mysql-server with the apt install command, we would watch how apt does its thing without the ability to do other stuff while that process is going on.
If you are used to windows OS, it can even be more annoying as we are used to multitasking. Of course, you can open a new terminal or shell and multi-task away, but it gets crazy when you have 5 terminal opened at a time, what if we can sort of background the process, or in other words, minimize the ongoing process and continue with other stuff.
In this guide, you will learn how to minimize processes, and work on other stuff while the process is still going on, this is similar to the way you would minimize a program on Windows or macOS, let's get started...
The scenario of what we would do are:
- Open a new file in a text editor
- Write a dummy or random words in the opened file
- Background the text-editor, think of minimizing Microsoft Word
- Confirm if the process is still running (the text editor)
- Open up Text editor 2, write random words, background it
- Bring up the first text editor, and save the file you are editing, background it and
- Bring back the second text editor, save the file you are editing.
Create a new file using the nano editor in your home directory, for example:
The ~/ would reference the user home directory, for example, if the current user home folder is James, it would create the file in /home/james/note-1
With the note-1 file opened, write any random text, I will be writing: This is my first note book
Alright, press Ctrl + Z on your keyboard to minimize the note-1 file.
You can see you were immediately taken away from the editor, and taken to the shell to do other stuff. If you've done everything right, you should see an output similar to below:
+ Stopped nano ~/note-1
The  is the job number of our process, Stopped is its status (more on this below), and lastly, it shows you are working on a file name note-1 in the home directory.
The stopped status means the process has been temporarily put into the background and is no longer attached to the current terminal (cannot produce output and is not receiving input from the user), but it is still using the system memory.
The TLDR version is stopped means the process is running in the background.
To confirm if the process is still running, run the following command:
ps au | grep nano
The above command is basically saying, look for a process named nano if running, and pipe the output into grep, the output is as follows:
user 5594 0.0 0.2 22280 4152 pts/0 T 21:31 0:00 nano /home/user/note-1
The PID of the above nano process is 5594, this would be different from yours.
Learn More on Displaying Running Processes In Ubuntu
As the process is minimized, you can go ahead and do other stuff, let's create an additional file.
With the note-2 file opened, write any random text, I will be writing: This is my second note book
Background it using Ctrl + Z
At this point, we haven't saved any of the files opened, which would get lost if we log out of the system. Let's save the first file in the text-editor, bring it up using the fg command.
It didn't bring up the first text editor, it instead brought up the most recent opened process (which is the opened note-2 file in the nano editor), background it and execute the jobs command, and you should see the following output:
- Stopped nano ~/note-1 + Stopped nano ~/note-2
The output shows that I have two nano sessions in use, one modifying the note-1 file, and the other modifying note-2 file.
To bring up the first nano session, you simply run the fg command followed by the Job ID:
Save the file you are editing using Ctrl + x and Y to confirm the changes.
If you re-run the jobs command you would see it only has one process left in the background, just type fg to bring it and up and save.
You can see how handy it is to background processes, you don't even have to use Ctrl + Z to background a process, you can background a process right when you execute it by entering a command with the ampersand symbol (&), for example, to open up a new a file in nano and background it immediately, use the following command:
nano ~/nano-3 &
This would open up a new file and background it immediately.
Note: Not all applications support background, in this case, you either use a multiplexer or even running a new instance of the terminal or shell.