Use the Bash shell on Linux to manage foreground and background processes. You can use Bash's job control functions and signals to make instruction execution more flexible. We show you how.
All About Processes
Each time a program runs under a Linux or Unix-like operating system, a process is started. "Process" is the name for the internal representation of the executed program in the memory of the computer. There is a process for each active program. In fact, there is a process for almost everything that runs on your computer. These include the components of your graphical desktop environment (GDE) such as GNOME or KDE, as well as system daemons that are launched at startup.
Why almost everything that is executed? Well, bash-built-ins like cd, pwd, and alias do not need to have a process started (or spawned) when executed. Bash executes these commands in the instance of the bash shell that runs in your terminal window. These commands are fast, just because no process needs to be started for their execution. (You can type
help in a terminal window to display the list of built-in bash functions.)
Processes can be performed in the foreground. In this case they take over your terminal until they are completed. or they can be run in the background. Processes that run in the background do not dominate the terminal window and you can continue working on it. At least they do not dominate the terminal window if they do not generate screen output.
A Chaotic Example
We will launch a simple
ping trace . We will pang
the how-to-geek domain . This is done as a foreground process.
We get the expected results by scrolling down the terminal window. We can not do anything else in the terminal window while
ping is running. To exit the command, press
Ctrl + C .
Ctrl + C
The visible effect of
Ctrl + C is highlighted in the screenshot.
ping gives a short summary and then stops.
Let's repeat that. This time, however, we're pressing
Ctrl + Z instead of
Ctrl + C . The task is not finished. It becomes a background task. We get control of the terminal window returned.
Ctrl + Z
The Visible Impact of Meeting
Ctrl + Z is highlighted in the screenshot.
This time we are told that the process is stopped. Stopped means not finished. It's like a car at a stop sign. We did not scrape it and throw it away. It is still on the road, stationary and waiting to go. The process is now a background job .
jobs lists the jobs started in the current terminal session. And because jobs are (inevitably) processes, we can also display them with the command
ps . Let's use both commands and compare their outputs. We use the option
T (terminal) to list only the processes running in this terminal window. Note that no hyphen
- must be used with option
 The command
jobs tells us:
- : The number in square brackets is the job number. We can use this to refer to the job if we need to control it with job control commands.
- + : The plus sign
+indicates that this is the job being edited if we use a job control command without a specific job number. It is called a standard job. The default job is always the one most recently added to the list of jobs.
- Stopped : The process is not running.
- ping www.howtogeek.com : The command line that started the process.
- PID : The process ID of the process. Every process has a unique ID.
- TTY : The pseudo-teletype (terminal window) from which the process was run.
- STAT : The status of the process.
- TIME : The CPU time consumed by the process.
- COMMAND : The command that started the process.
These are general values for the STAT column:
- D : Uninterrupted Sleep. The process is in a wait state, usually waiting for input or output and can not be interrupted.
- I : Idle.
- R : Runs.
- S : Intermittent sleep.
- T : Stopped by a job control signal.
- Z : A zombie process. The process was aborted but not "purged" by its parent process.
The value in the STAT column can be followed by one of the following additional indicators:
- <: High priority task (Not nice to other processes).
- N : Low priority (nice to other processes).
- L : The process contains pages stored in memory (usually used by real-time processes).
- s : A session leader. A session leader is a process that has started process groups. A shell is a session leader.
- l : Multithreaded process.
- + : foreground process.
We can see that Bash has the status
ss [19459015hat]. The capital letter "S" indicates that the bash shell sleeps and can be interrupted. As soon as we need it, it will answer. The lowercase letter "s" indicates that the shell is a session leader.
The ping command has the status
T . This tells us that
ping was stopped by a job control signal. In this example, this was the
Ctrl + Z with which we put it in the background.
ps T has the status
R . That stands for running. The
+ indicates that this process is a member of the foreground group. The command
ps T is executed in the foreground.
The command bg
bg is used to continue a background process. It can be used with or without order number. If you use it without an order number, the standard order will be brought to the foreground. The process is still running in the background. You can not send any input.
If we issue the command
bg we continue our command
ping continues, and the terminal window scrolls again. The name of the command that was restarted will be displayed. This is highlighted in the screenshot.
But we have a problem. The task runs in the background and does not accept input. So how can we stop it?
Ctrl + C does not matter. We can see it when we type it, but the background task does not receive these keystrokes, so it keeps ringing happily.
We are now in a strange mixed mode. We can type in the terminal window, but what we type is quickly deleted by the scrolling output of the command
ping . Everything we enter becomes the foreground.
To stop our background task, we have to put it in the foreground and then stop it.
The command fg
fg will bring a background task to the fore. Just like the command
bg it can be used with or without an order number. Using an order number means that a particular order is being processed. When used without a job number, the last command sent to the background is used.
If you enter
ping command will be placed in the foreground. The characters we enter are confused with the output of the command
ping but are treated by the shell as if they had been typed on the command line as usual. And that's exactly what happened with the bash shell.
And now we have it If the command
ping is run again in the foreground, you can use it with
Exit Ctrl + C .
Ctrl + C
We must send the correct signals
That was not exactly pretty. Obviously, running a process in the background works best if the process does not produce any output and does not require input.
However, our example has achieved the following:
- Put a process in the background.
- ] Restore the running status of the process in the background.
- Resetting the process to the foreground.
- Stop the process.
If you use
Ctrl + C and
Ctrl + Z They send signals to the process. These are abbreviations for the command
kill . There are 64 different signals that can send
kill . Use
kill -l on the command line to list it.
kill is not the only source of these signals. Some of them are automatically triggered by other processes in the system.
Here are some of the most used ones.
- SIGHUP : Signal 1. Automatically sends to a process when the terminal in which it is running is closed.
- SIGINT : Signal 2. Sent to a process that you called with
Ctrl + C. The process is interrupted and prompted to quit.
- SIGQUIT : Signal 3. Sent to a process when the user sends a termination signal
Ctrl + D.
- SIGKILL : Signal 9. The process is aborted immediately and no attempt is made to terminate it properly. The process does not run properly.
- SIGTERM : Signal 15. This is the standard signal sent by
kill. This is the standard program termination signal.
- SIGTSTP : Signal 20. Sent to a process using
Ctrl + Z. It stops the process and puts it in the background.
kill must output signals that have not been assigned a key combination.
Further Job Control
A process with
moved into the background Ctrl + Z is put into the stopped state. We must use the command
bg to restart it. It's easy to start a program as a running background process. Add an ampersand
and to the end of the command line.
Although it is best that background processes do not write to the terminal window, we will use examples that do so. The screenshots must contain something that we can refer to. This command starts an infinite loop as a background process:
true; Echo "How-To Geek Loop Process"; sleep 3; done &
The order number and the process ID of the process will be communicated to us. Our order number is 1 and the process ID is 1979. With these IDs we can control the process.
The output of our infinite loop is displayed in the terminal window. As before, we can use the command line, but all commands issued by us are interspersed with the output of the loop process.
 To stop our process, we can remember the job number with
Jobs and then kill with
kill we must preface it with a percent sign
kill sends the signal
SIGTERM signal number 15 to the Process and is finished. The next time you press Enter, the status of the job is displayed. The process is listed as "completed". If the process does not respond to the command
kill you can go one level higher. Use
SIGKILL signal number 9. Simply substitute the number 9 between the command
kill and the job number.
kill 9% 1
Things we treated
- Ctrl + C : Sends
SIGINTSignal 2 to the process - if he accepts input - and assigns it to finish it.
- Ctrl + D : Sends
SISQUITSignal 3 to the process - if he accepts input - and instructs him to finish it.
- Ctrl + Z : Sends
SIGSTPSignal 20 to the process and instruct it to stop and become a background process.
- jobs : Lists the background jobs and displays their job number.
- bg job_number : Restarts a background process. If you do not specify a job number, the last process that has been converted to a background task is used.
- fg job_number : Brings a background process to the foreground and restarts it. If you do not specify an order number, the last process that has been converted to a background task is used.
- Command Line & : Adding an Et character
&to the end of a command line runs this command as a background task that is running.
- kill% job_number : Sends
SIGTERMSignal 15, to the process to end it.  kill 9 % job_number : Sends
SIGKILLSignal 9, to the process and terminates it abruptly.
RELATED: How to terminate processes from the Linux terminal