loading...

Ubuntu Server 18.04 – Managing jobs

Installing an FTP server

Up until now, everything we have been doing on the shell has been right in front of us, from execution to completion. We’ve installed applications, run programs, and walked through various commands. Each time, we’ve had control of our shell taken from us, and we’ve only been able to start a new task when the previous one had finished. For example, if we were to install the vim-nox package with the apt install command, we would watch helplessly while apt takes care of fetching the package and installing it for us. While this is going on, our cursor goes away and our shell completes the task for us without allowing us to queue up another command. We can always open a new shell to the server and multi-task by having two windows open at once, each doing different tasks. Actually, we don’t have to do that, unless we want to. We can actually background a process without waiting for it to complete while working on something else. Then, we can bring that process back to the front to return to working on it or to check whether or not it finished successfully. Think of this as a similar concept to a windowing desktop environment, or user interfaces on the Windows or macOS operating systems. We can work on an application, minimize it to get it out of the way, and then maximize it to continue working with it. Essentially, that’s the same concept of backgrounding a process in a Linux shell.

In my opinion, the easiest way to learn a new concept is to try it out, and the easiest example I can think of is by using a text editor. In fact, this example is extremely useful and may just become a part of your daily workflow. To do this exercise, you can use any command-line text editor you prefer, such as vim or nano. On Ubuntu Server, nano is usually installed by default, so you already have it if you want to go with that. If you prefer to use vim, feel free to install the vim-nox package:

sudo apt install vim-nox 
If you want to use vim, you can actually install vim rather than vim-nox, but I always default to vim-nox since it features built-in support for scripting languages.

Again, feel free to use whichever text editor you want. Teaching you how to use a text editor is beyond the scope of this tutorial, so just use whatever you feel comfortable with. In the following examples, I’ll be using nano. However, if you use vim, just replace nano with vim every time you see it.

Anyway, to see backgrounding in action, open up your text editor. Feel free to open a file or just start a blank session. (If in doubt, type nano and press Enter.) With the text editor open, we can background it at any time by pressing Ctrl + Z on our keyboard.

If you are using vim instead of nano, you can only background vim when you are not in insert mode, since it captures Ctrl + Z rather than passing it to the shell.

Did you see what happened? You were immediately taken away from your editor and were returned to the shell so you could continue to use it. You should have seen some output similar to the following:

[1]+ Stopped nano 

Here, we see the job number of our process, its status, and then the name of the process. Even though the process of your text editor shows a status of Stopped, it’s still running. You can confirm this with the following command:

ps au |grep nano 

In my case, I see the nano process running with a PID of 1070:

jay 1070 0.0 0.9 39092 7320 pts/0 T 15:53 0:00 nano 

At this point, I can execute additional commands, navigate around my filesystem, and get additional work done. When I want to bring my text editor back, I can use the fg command to foreground the process, which will resume it. If I have multiple background processes, the fg command will bring back the one I was working on most recently.

I gave you an example of the ps command to show that the process was still running in the background, but there’s actually a dedicated command for that purpose, and that is the jobs command. If you execute the jobs command, you’ll see in the output a list of all the processes running in background. Here’s some example output:

[1]- Stopped nano file1.txt 
[2]+ Stopped nano file2.txt 

The output shows that I have two nano sessions in use, one modifying file1.txt, and the other modifying file2.txt. If I were to execute the fg command, that would bring up the nano session that’s editing file2.txt, since that was the last one I was working in. That may or may not be the one I want to return to editing, though. Since I have the job ID on the left, I can bring up a specific background process by using its ID with the fg command:

fg 1 

Knowing how to background a process can add quite a bit to your workflow. For example, let’s say, hypothetically, that I’m editing a config file for a server application, such as Apache. While I’m editing this config file, I need to consult the documentation (man page) for Apache because I forgot the syntax for something. I could open a new shell and an SSH session to my server and view the documentation in another window. This could get very messy if I open up too many shells. Better yet, I can background the current nano session, read the documentation, and then foreground the process with the fg command to return to working on it. All from one SSH session!

To background a process, you don’t have to use Ctrl + Z; you can actually background a process right when you execute it by entering a command with the ampersand symbol (&) typed at the end. To show you how this works, I’ll use htop as an example. Admittedly, this may not necessarily be the most practical example, but it does work to show you how to start a process and have it backgrounded right away. We’ll get to the htop command later in this chapter, but for now feel free to install this package and then run it with the ampersand symbol:

sudo apt install htop 
htop & 

The first command, as you already know, installs the htop package on our server. With the second command, I’m opening htop but backgrounding it immediately. What I’ll see when it’s back-grounded is its job ID and PID. Now, at any time, I can bring htop to the foreground with fg. Since I just backgrounded it, fg will bring htop back since it considers it the most recent. As you know, if it wasn’t the most recent, I could reference its job ID with the fg command to bring it back even if it wasn’t my most recently used job. Go ahead and practice using the ampersand symbol with a command and then bringing it back to the foreground. In the case of htop, it can be useful to start it, background it, and then bring it back anytime you need to check the performance of your server.

Keep in mind, though, that when you exit your shell, all your backgrounded processes will close. If you have unsaved work in your vim sessions, you’ll lose what you were working on. For this reason, if you utilize background processes, you may want to check to see if you have any pending jobs still running by executing the jobs command before logging out.

In addition, you’ll probably notice that some applications background cleanly, while others don’t. In the case of using a text editor and htop, those applications stay quietly running in the background, allowing us to perform other tasks and allowing us to return to those commands later. However, some applications may still spit out diagnostic text regularly in your main window, whether they’re backgrounded or not. To get even more control over your bash sessions, you can learn how to use a multiplexer, such as tmux or screen, to allow these processes to run in their own session such that they don’t interrupt your work. Going over the use of a program such as tmux is beyond the scope of this tutorial, but it is a useful utility to learn if you’re interested.

Comments are closed.

loading...