How to Run and Control Background Processes on Linux

A shell environment on a Linux computer.Fatmawati Achmad Zaenuri / Shutterstock.com

Use the Bash Shell under Linux to manage the foreground and background processes. You can use Bash's functions and job control signals to give you more flexibility in order execution. We show you how.

All about processes

When a program is running in a Linux or Unix operating system, a process is started. "Process" is the name of the internal representation of the program running in the computer's memory. There is a process for each active program. In fact, there is a process for almost everything that is running on your computer. This includes the components of your graphical desktop environment (GDE) such as GNOME or KDEand system demons which are started at startup.

Why almost everything that turns? Well, the built-in Bash as CD, pwd, and alias do not need to have a process launched (or "spawn") when they are executed. Bash executes these commands in the instance of the Bash shell running in your terminal window. These commands are fast precisely because they do not need a running process to run. (You can type help in a terminal window to display the list of built-in Bash functions.)

Processes can be run in the foreground, in which case they support your device until completion or can run in the background. Processes running in the background do not dominate the terminal window, and you can continue to work on it. Or at least they do not dominate the terminal window if they do not generate an output on the screen.

A messy example

We will start a simple ping trace in progress. We will ping the How-To Geek domain. This will run as a leading process.

ping www.howtogeek.com

ping www.howtogeek.com in a terminal window

We get the expected results by scrolling through the terminal window. We can not do anything else in the terminal window while ping is running. To end the order, press Ctrl + C.

Ctrl + C

Ping trace output in a terminal window

The visible effect of Ctrl + C is highlighted in the screen capture. ping gives a brief summary then stops.

Repeat this. But this time, we will press Ctrl + Z instead of Ctrl + C. The task will not be completed. This will become a substantive task. We get control of the terminal window.

ping www.howtogeek.com
Ctrl + Z

effect of Ctrl + Z on a command executing in a terminal window

The visible effect of hitting Ctrl + Z is highlighted in the screen capture.

This time, we are told that the process is stopped. Stopped does not mean finished. It's like a car at a stop. We did not abandon it and throw it in the trash. He is always on the road, at a standstill, waiting. The process is now a work in the background.

The jobs command will list the jobs that were started in the current terminal session. And since tasks are (inevitably) processes, we can also use the ps command to see them. Let's use both commands and compare their outputs. We will use the T (terminal) option to list only processes that are running in this terminal window. Note that there is no need to use a hyphen – with the option T.

jobs
PST

command jobs in a terminal window

The jobs command tells us:

[1]: The number in brackets is the job number. We can use it to refer to work when we have to control it with the help of work control commands.
+: The plus sign + indicates that this is the job we will be working on if we use a job control command with no specific job number. This is what is called the default job. The default job is always the one most recently added to the job list.
Stopped: The process is not running.
ping www.howtogeek.com: The command line that started the process.

The ps command tells us:

PID: The identifier of the process. Each process has a unique identifier.
ATS: Pseudo-teletype (terminal window) from which the process was executed.
STAT: The status of the process.
TIME: The amount of CPU time consumed by the process.
ORDER: The command that started the process.

These are common values ​​for the STAT column:

re: Sleep without interruption. The process is in a state of waiting, usually waiting for an entry or exit, and can not be interrupted.
I: Inactive.
R: Operation.
S: Interruptible sleep.
T: Stopped by a job control signal.
Z: A zombie process. The process is complete but has not been "cleaned up" by its parent process.

The value in the STAT column can be followed by one of these additional indicators:

<: Priority task (not nice for other processes).
NOT: Low priority (nice for other processes).
The: process has pages locked in memory (usually used by real-time processes).
s: A session leader. A session manager is a process that has initiated process groups. A shell is a session leader.
l: Multi-threaded process.
+: A leading process.

We can see that Bash has a state of Ss.The capital "S" tells us that the Bash shell is sleeping and that it is interruptible. As soon as we need it, he will answer. The lowercase "s" tells us that the shell is a session leader.

The ping command has the status T. This tells us that the ping command was stopped by a job control signal. In this example, it was the Ctrl + Z we used to put it in the background.

The ps T command is in the R state, which means execution. The + indicates that this process is a member of the leading group. So the ps T command is running in the foreground.

The bg command

The bg command allows you to resume a process in the background. It can be used with or without a work number. If you use it without a work number, the default job is brought to the fore. The process is always running in the background. You can not send him any entry.

If we issue the bg command, we will resume our ping command:

bg

bg in a terminal window

The ping command resumes and we see the scrolling output again in the terminal window. The name of the command that has been restarted is displayed for you. This is highlighted in the screen capture.

recovery of the ping background process with exit in a terminal widow

But we have a problem. The task runs in the background and does not accept entries. So, how can we stop it? Ctrl + C does not do anything. We can see it when we type it, but the background task does not receive these keystrokes and therefore continues to ping.

Ignoring background task Ctrl + C in a terminal window

In fact, we are now in a strange mixed mode. We can type in the terminal window but what we type is quickly scanned by the ping's scrolling result. Everything we type takes effect in the previous one.

To stop our background task, we must bring it to the forefront and then stop it.

The fg command

The fg command will bring a background task to the foreground. Like the bg command, it can be used with or without a work number. Its use with a work number means that it will work on a specific job. If it is used without a work number, the last command sent to the background is used.

If we type fg, our ping command will be put in the foreground. The characters we type are mixed with the result of the ping command, but they are exploited by the shell as if they were entered as usual on the command line. And in fact, from the point of view of the Bash shell, that's exactly what happened.

fg

Mixed fg command with ping output in a terminal window

And now that we have the ping command running in the foreground again, we can use Ctrl + C to kill it.

Ctrl + C

Ctrl + C stop pinging in a terminal window

We must send the right signals

It was not really pretty. It is obvious that running a background process works best when the process does not produce output and does not need input.

But, in disorder or not, our example has accomplished:

Put a process in the background.
Restore the process to a running state in the background.
Return the process to the fore.
Finish the process.

When you use Ctrl + C and Ctrl + Z, you send signals to the process. These are shortcut ways to use the kill command. There is 64 different signals this kill can send. Use kill -l on the command line to list them. Kill is not the only source of these signals. Some are automatically generated by other system processes.

Here are some of the most commonly used ones.

SIGHUP: Signal 1. Sent automatically to a process when the terminal in which it is running is closed.
SIGINT: Signal 2. Sent to a process that you hit Ctrl + C. The process is interrupted and you are asked to terminate.
SIGQUIT: Signal 3. Sent to a process if the user sends a closing signal Ctrl + D.
SIGKILL: Signal 9. The process is immediately killed and will not attempt to close properly. The process is not going well.
SIGTERM: Signal 15. This is the default signal sent by kill. This is the standard end of program signal.
SIGTSTP: Signal 20. Sent to a process when you use Ctrl + Z. It stops the process and puts it in the background.

We must use the kill command to send signals to which no key combination is assigned to them.

Additional work control

A process moved in the background with the help of Ctrl + Z is placed in the stopped state. We need to use the bg command to run it again. Starting a program as a background process is simple. Add an ampersand & at the end of the command line.

Although it is better that the background processes do not write in the terminal window, we will use examples. We need to have something in the screenshots. This command will launch an endless loop as a background process:

although true; echo "How-To Geek Loop Process"; to sleep 3; completed &

We are told the work number and the process ID of the process. Our work number is 1 and the process ID is 1979. We can use these identifiers to control the process.

The output of our endless loop begins to appear in the terminal window. As before, we can use the command line, but all the commands we issue are interspersed with the output of the loop process.

ls

exit from the background loop process interspersed with the output of other commands

To stop our process, we can use tasks to remind us of the task number and then use kill.

jobs indicates that our process is job number 1. To use this number with kill, we must precede it with a% sign.

jobs
kill% 1

jobs and kill% 1 in a terminal window

kill sends the signal SIGTERM, number 15, to the process and it is finished. When you press the Enter key, the status of the job is displayed. It indicates that the process is "finished". If the process does not respond to the kill command, you can raise it. Use kill with SIGKILL, signal number 9. Simply put the number 9 between the kill command and the job number.

kill 9% 1

Things we covered

Ctrl + C: Send SIGINT, signal 2, to the process (if it accepts the input) and tell it to end.
Ctrl + D: Sends the signal 3 SISQUIT to the process – if it accepts the input – and tells it to leave.
Ctrl + Z: Send SIGSTP, signal 20, to the process and tell it to stop (suspend) and become a process in the background.
jobs: Lists the tasks in the background and displays their number.
bg job_number: Restarts a process in the background. If you do not provide a job number, the last process transformed into a background job is used.
fg job_number: brings a background process to the foreground and restarts it. If you do not provide a job number, the last process transformed into a background job is used.
command line And: Adding an ampersand & at the end of a command line executes this command as a background task, being executed.
kill%job_number: sends SIGTERM, signal 15, to the process to complete it.
kill 9 %job_number: sends SIGKILL, signal 9, to the process and terminates it abruptly.

RELATED, RELATED, RELATED: How to kill Linux terminal processes

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.