11 minutes read

In old times when people had to use only a terminal and only one terminal was accessible, some utilities were invented to make busy terminals available for work. Now we have more options. For example, most people usually don't use the terminal to open programs with a graphical user interface (GUI), and we can open a few terminal windows or tabs to do more than one thing at a time.

However, there are times and situations when these "old tools" can come in handy. For example, you start a program from the terminal and while it is running you decide to use the same terminal window to do something else. Terminals are also pretty useful when you are working with scripts. So, let's check out how these tools can manage programs sending them to the background and foreground, and what sort of programs are always hiding in the background.

Foreground and background

Foreground, background... You may be wondering, what's this all about? Simply put, it's about the shell. The process that occupies the shell and blocks the work in it is running in the foreground. By default, all programs are running in the foreground, and, usually, they produce some output in the terminal. On the other hand, a process running in the background is the one that doesn't block the shell making it possible for you to use it.

Managing processes this way is possible due to the shell's ability of job control: a job represents one process or a group of processes controlled by a particular shell. Actually, jobs are child processes of the shell. Each job has its own number and since a job is also a process, it has a process ID (PID).

Now let's take a quick look at some important programs that are always running in the background.

Daemons – hidden workers

Programs starting processes while the system is booting, monitoring network or work of devices, and a lot of other necessary stuff in Unix-like systems, are called daemons. They work as processes in the background, usually without the user starting them. The name came from Maxwell's thought experiment, where the daemon was opening and closing the door controlling the flow of slow and fast molecules between two chambers with gas.

Usually but not always you can distinguish them from other processes by their name, they have a letter "d" at the end like rsyslogd, systemd, crond.

systemd is one of the most known daemons. It starts the whole system on most GNU/Linux distributions and monitors it after that. Actually, it is the biggest daemon that manages other daemons. We've already mentioned other daemons: rsyslogd manages system logs and crond is a task scheduler daemon.

It is possible to manually manage daemons by starting them or restarting, reloading, checking status, enabling, or disabling them while booting the system, but that is a whole other story.

Going to the background

If you open a program from the terminal, just by typing its name, you'll see that prompt in the shell is not returned and the shell is not responding. We can only suspend the program with Ctrl-z or close it with Ctrl-c from the shell. Actually, sometimes even that won't work.

However, If you want to open a program from the terminal in the background, type the ampersand symbol & after the program's name separated by space. For example, you can open a web-browser qutebrowser in the background from the terminal like this:

$ qutebrowser &

If you forgot to put your program in the background and opened it without a & symbol, then you don't need to close it and open it again to put it in the background. You can do the following:

  1. suspend the program with Ctrl-z, the output in the terminal will be something like this where the number in square brackets is the number of the job;
    [1]+  Stopped                 qutebrowser
  2. then simply type the percent symbol % after bg followed by the number of the job (in our case it's 1), or just type bg alone as it will take the last suspended job number by default.
    $ bg %1
    
    [1]+ qutebrowser &

After this, the command terminal will show that job 1 was added to the background.

A word about scripts

PIDs are extremely useful when you need to manage programs in scripts because job numbers, Ctrl-z, and Ctr-c can hardly be used there. There is a command with a dollar symbol and an exclamation point $! that might come in handy. It's taking the PID of the last program sent to the background by the user. You can store the PID of some job in the variable, say, like below with the PID of qutebrowser and use it later.

qutebrowser &
QUTEBROWSER_PID=$!
kill $QUTEBROWSER_PID

Returning to the foreground

What if you need to return the program, i.e. the job, from the background to the foreground? If you wonder when you might need that, it is when the program in the background is not responding to any command from the terminal, including commands Ctrl-c and Ctrl-z. So to manage the program from the terminal you can use the command fg in the same way as bg before. Type fg and number of the job after the % sign (or just type fg alone as it will take the last job number by default). That's how it's done with job number 1:

$ fg %1

By the way, you can monitor jobs started from the current terminal and their status by typing the jobs command.

Here is an example of that with the output:

$ jobs

[1]-  Running                 qutebrowser &
[2]+  Running                 libreoffice &

Conclusion

The most important stuff that you should remember:

  • to open a program in the background use & command after the name of the program;
  • to send the working program to the background, suspend it (Ctrl-z), and use bg command with job number after %;
  • to send a program from the background to the foreground use fg command with job number after %.
15 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo