Introduction
In Unix-type systems, each command running from the terminal creates a process. By default, these processes are run in the foreground, which means the shell is waiting for them to finish before accepting new orders. However, there are situations in which it is useful to allow a process to continue as we continue to work on the terminal. To do this Linux has the commandsbgandfg, which allow to move processes between foreground and foreground.
What are the processes in the first and second levels
A foreground process blocks the terminal: no other command can be written until it is finished or suspended. A background process, on the other hand, continues to run but the shell is available immediately. This is achieved by suspension (withCtrl + Z) and the subsequent resumption in the background withbgeither by launching the process directly with the operator&at the end of the line.
How to send a process to the background with & and bg
The easiest way to start a background process is to add the symbol&at the end of the command. For example:
$ find / -name "*.log "2 > / dev / null & [1] 12345
The shell shows the working number (in square brackets) and the PID. The process continues to run while the terminal is free for other commands.
If you already have a process running and you want to go to the background without finishing it, first you suspend it withCtrl + Z. The shell will show something like:
$ find / -name "*.tmp "2 > / dev / null ^ Z [1] + Stopped find / -name"*. "2 > / dev / null
Then with the commandbgyou indicate that it continues to be executed in the background:
$ bg [1] + find / -name "*.tmp "2 > / dev / null &
From that moment on the process continues to run and the terminal is available.
Bring a process to the foreground with fg
When you need to interact again with a process that is in the background (for example, to provide input or see its output in real time), you bring it to the foreground usingfg. If there is only one job in the background, just write:
$ fg
If there are several jobs, you can specify the work number:
$ fg% 2
The process will re-block the terminal until you finish or suspend it again withCtrl + Z.
Practical examples
- Background file compression:
$ tar -czf copia.tar.gz /home/usuario/documentos & - Running a long script and tracking your output:
$ . / script _ largo.sh ^ Z [1] + Stopped. / script _ long. sh$ bg [1] +. / script _ largo.sh &$ jobs [1] + Running. / script _ largo.sh &$ fg
- Change between multiple jobs:
$ ping -c 100 8.8.8.8 & [1] 6789$ sleep 60 & [2] 6790$ jobs [1] - Running ping -c 100 8.8.8.8 & [2] + Running sleep 60 &$ fg% 1 # brings the ping to the foreground ^ Z [1] + Stopped ping -c 100 8.8.8.8$ bg% 1 # makes it back to background [1] + ping -c 100 8.8.8.8 &
Considerations and good practices
- The background processes continue to consume CPU resources and memory; it monitors their use in systems with limitations.
- The works that try to read from the standard input (
stdin) will automatically stop when they try to read and no data are available; in such cases it is necessary to bring them to the foreground to provide the input. - Use the command
jobsto list the current works and their states. - If you close the terminal while there are work in the background, they will receive the signal.
SIGHUPand will be finished unless you have disassociated them withdisownor you started them withnohup. - For processes that should continue to be implemented after closing the session, combine
nohupwith&or usedisownafter putting them in the background.
Conclusion
Domain the commandsbgandfgIt allows you to efficiently manage the execution of long tasks or that do not require constant interaction, keeping the terminal free for other operations. Learning to suspend, resume and change from first to second is a fundamental skill for any Linux user or administrator who wants to work productively from the command line.


