How to run and control Background processes on Linux

Configurare noua (How To)

Situatie

Use the Bash shell in Linux to manage foreground and background processes. You can use Bash’s job control functions and signals to give you more flexibility in how you run commands.

Solutie

Processes can be running in the foreground, in which case they take over your terminal until they have completed, or they can be run in the background. Processes that run in the background don’t dominate the terminal window and you can continue to work in it. Or at least, they don’t dominate the terminal window if they don’t generate screen output.

A Messy Example

We’ll start a simple ping trace running. We’re going to ping the How-To Geek domain. This will execute as a foreground process.

We get the expected results, scrolling down the terminal window. We can’t do anything else in the terminal window while ping is running. To terminate the command hit Ctrl+C.

The visible effect of the Ctrl+C is highlighted in the screenshot. ping gives a short summary and then stops.

Let’s repeat that. But this time we’ll hit Ctrl+Z instead of Ctrl+C. The task won’t be terminated. It will become a background task. We get control of the terminal window returned to us.

The visible effect of hitting Ctrl+Z is highlighted in the screenshot. This time we are told the process is stopped. Stopped doesn’t mean terminated. It’s like a car at a stop sign. We haven’t scrapped it and thrown it away. It’s still on the road, stationary, waiting to go. The process is now a background job. The jobs command will list the jobs that have been started in the current terminal session. And because jobs are (inevitably) processes, we can also use the ps command to see them. Let’s use both commands and compare their outputs. We’ll use the T option (terminal) option to only list the processes that are running in this terminal window. Note that there is no need to use a hyphen – with the T option.

The jobs command tells us:

[1]: The number in square brackets is the job number. We can use this to refer to the job when we need to control it with job control commands.

+: The plus sign + shows that this is the job that will be acted upon if we use a job control command without a specific job number. It is called the default job. The default job is always the one most recently added to the list of jobs.

Stopped: The process is not running.

The ps command tells us:

PID: The process ID of the process. Each process has a unique ID.

TTY: The pseudo-teletype (terminal window) that the process was executed from.

STAT: The status of the process.

TIME: The amount of CPU time consumed by the process.

COMMAND: The command that launched the process.

These are common values for the STAT column:

These are common values for the STAT column:

D: Uninterruptible sleep. The process is in a waiting state, usually waiting for input or output, and cannot be interrupted.
I: Idle.
R: Running.
S: Interruptible sleep.
T: Stopped by a job control signal.
Z: A zombie process. The process has been terminated but hasn’t been “cleaned down” by its parent process.

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

<: High-priority task (not nice to other processes).

N: Low-priority (nice to other processes).

L: process has pages locked into memory (typically used by real-time processes).

s: A session leader. A session leader is a process that has launched process groups. A shell is a session leader.

l: Multi-thread process.

+: A foreground process.

We can see that Bash has a state of Ss. The uppercase “S” tell us the Bash shell is sleeping, and it is interruptible. As soon as we need it, it will respond. The lowercase “s” tells us that the shell is a session leader. The ping command has a state of T. This tells us that ping has been stopped by a job control signal. In this example, that was the Ctrl+Z we used to put it into the background. The ps T command has a state of R, which stands for running. The + indicates that this process is a member of the foreground group. So the ps T command is running in the foreground.

The bg Command

The bg command is used to resume a background process. It can be used with or without a job number. If you use it without a job number the default job is brought to the foreground. The process still runs in the background. You cannot send any input to it.

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

But we have a problem. The task is running in the background and won’t accept input. So how do we stop it? Ctrl+C doesn’t do anything. We can see it when we type it but the background task doesn’t receive those keystrokes so it keeps pinging merrily away.

In fact, we’re now in a strange blended mode. We can type in the terminal window but what we type is quickly swept away by the scrolling output from the ping command. Anything we type takes effect in the foregound. To stop our background task we need to bring it to the foreground and then stop it.

The fg Command

The fg command will bring a background task into the foreground. Just like the bg command, it can be used with or without a job number. Using it with a job number means it will operate on a specific job. If it is used without a job number the last command that was sent to the background is used. If we type fg our ping command will be brought to the foreground. The characters we type are mixed up with the output from the ping command, but they are operated on by the shell as if they had been entered on the command line as usual. And in fact, from the Bash shell’s point of view, that is exactly what has happened.

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

Tip solutie

Permanent

Voteaza

(0 din 5 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?